Skip to content

Commit 501419d

Browse files
committed
List and open requests inside collections
1 parent 460f121 commit 501419d

File tree

4 files changed

+120
-14
lines changed

4 files changed

+120
-14
lines changed

data/ui/main_window.blp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,22 @@ template $CarteroWindow: Adw.ApplicationWindow {
4444
$CarteroSidebar collections {}
4545
};
4646

47-
content: Gtk.Box {
48-
orientation: vertical;
47+
content: Adw.ToolbarView {
48+
[top]
49+
Gtk.Box {
50+
orientation: vertical;
4951

50-
Adw.HeaderBar {
51-
[title]
52-
Label main_label {}
53-
}
52+
Adw.HeaderBar {
53+
[title]
54+
Label main_label {}
55+
}
5456

55-
Adw.ToastOverlay toaster {
5657
Adw.TabBar tabs {
5758
view: tabview;
5859
}
60+
}
5961

62+
Adw.ToastOverlay toaster {
6063
Adw.TabView tabview {}
6164
}
6265
};

src/fs/collection.rs

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
//
1616
// SPDX-License-Identifier: GPL-3.0-or-later
1717

18-
use std::path::Path;
18+
use std::fs::DirEntry;
19+
use std::path::{Path, PathBuf};
1920
use std::{fs::File, io::Write};
2021

2122
use crate::{error::CarteroError, objects::Collection};
@@ -45,3 +46,48 @@ pub fn open_collection(path: &Path) -> Result<Collection, CarteroError> {
4546
let metadata: Metadata = toml::from_str(&metadata_content)?;
4647
Ok(metadata.into())
4748
}
49+
50+
fn is_cartero_request(entry: &Result<DirEntry, std::io::Error>) -> bool {
51+
let Ok(entry) = entry else {
52+
return false;
53+
};
54+
let path = entry.path();
55+
if !path.is_file() {
56+
return false;
57+
}
58+
let Some(ext) = path.as_path().extension() else {
59+
return false;
60+
};
61+
ext.to_str().is_some_and(|s| s == "cartero")
62+
}
63+
64+
fn is_cartero_folder(entry: &Result<DirEntry, std::io::Error>) -> bool {
65+
let Ok(entry) = entry else {
66+
return false;
67+
};
68+
let path = entry.path();
69+
if !path.is_dir() {
70+
return false;
71+
}
72+
73+
let metadata = path.join("cartero-metadata");
74+
metadata.exists() && metadata.is_file()
75+
}
76+
77+
pub fn list_folders(path: &Path) -> Result<Vec<PathBuf>, CarteroError> {
78+
let folders = path
79+
.read_dir()?
80+
.filter(is_cartero_folder)
81+
.map(|entry| entry.unwrap().path())
82+
.collect();
83+
Ok(folders)
84+
}
85+
86+
pub fn list_endpoints(path: &Path) -> Result<Vec<PathBuf>, CarteroError> {
87+
let endpoints = path
88+
.read_dir()?
89+
.filter(is_cartero_request)
90+
.map(|entry| entry.unwrap().path())
91+
.collect();
92+
Ok(endpoints)
93+
}

src/widgets/sidebar.rs

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,22 @@ use crate::{
2626
};
2727

2828
mod imp {
29+
use std::path::PathBuf;
30+
2931
use adw::subclass::bin::BinImpl;
3032
use glib::subclass::InitializingObject;
3133
use glib::Object;
3234
use gtk::gio::{ListModel, ListStore};
3335
use gtk::subclass::prelude::*;
3436
use gtk::{
3537
prelude::*, CompositeTemplate, ListView, SignalListItemFactory, SingleSelection,
36-
TreeExpander, TreeListModel,
38+
TreeExpander, TreeListModel, TreeListRow,
3739
};
3840

39-
use crate::objects::{KeyValueItem, TreeNode, TreeNodeKind};
41+
use crate::fs::collection::{list_endpoints, list_folders};
42+
use crate::objects::{TreeNode, TreeNodeKind};
4043
use crate::widgets::sidebar_row::SidebarRow;
44+
use crate::win::CarteroWindow;
4145

4246
#[derive(CompositeTemplate, Default)]
4347
#[template(resource = "/es/danirod/Cartero/sidebar.ui")]
@@ -81,10 +85,41 @@ mod imp {
8185
let root_model: ListStore = Object::builder()
8286
.property("item-type", TreeNode::static_type())
8387
.build();
84-
TreeListModel::new(root_model, false, false, |_obj: &Object| {
88+
TreeListModel::new(root_model, false, false, |obj: &Object| {
89+
let tree_node = obj.downcast_ref::<TreeNode>()?;
90+
91+
if tree_node.node_type() == TreeNodeKind::Endpoint {
92+
return None;
93+
}
94+
95+
let path_buf = PathBuf::from(tree_node.path());
96+
let folders = list_folders(&path_buf);
97+
let child = list_endpoints(&path_buf);
98+
println!("{:?}", child);
99+
85100
let children: ListStore = Object::builder()
86-
.property("item-type", KeyValueItem::static_type())
101+
.property("item-type", TreeNode::static_type())
87102
.build();
103+
104+
if let Ok(folders) = folders {
105+
for f in folders {
106+
let node = TreeNode::default();
107+
node.set_path(f.to_str().unwrap());
108+
node.set_title(f.file_name().unwrap().to_str().unwrap());
109+
node.set_node_type(TreeNodeKind::Folder);
110+
children.append(&node);
111+
}
112+
}
113+
if let Ok(child) = child {
114+
for c in child {
115+
let node = TreeNode::default();
116+
node.set_path(c.to_str().unwrap());
117+
node.set_title(c.file_name().unwrap().to_str().unwrap());
118+
node.set_node_type(TreeNodeKind::Endpoint);
119+
children.append(&node);
120+
}
121+
}
122+
88123
let model = children.upcast::<ListModel>();
89124
Some(model)
90125
})
@@ -100,8 +135,22 @@ mod imp {
100135

101136
#[template_callback]
102137
fn on_activate(list: ListView, pos: u32, data: &Object) {
103-
println!("activate()");
104-
println!("list = {:?} \n pos = {:?} \n data = {:?}", list, pos, data);
138+
let window = list.root().and_downcast::<CarteroWindow>().unwrap();
139+
let Some(model) = list.model() else {
140+
return;
141+
};
142+
143+
let row = model.item(pos).and_downcast::<TreeListRow>().unwrap();
144+
let inner_value = row.item().and_downcast::<TreeNode>().unwrap();
145+
146+
let path = inner_value.path();
147+
148+
match inner_value.node_type() {
149+
TreeNodeKind::Endpoint => {
150+
window.open_endpoint(&path);
151+
}
152+
_ => println!("Not implemented yet, wait a minute"),
153+
}
105154
}
106155

107156
#[template_callback]

src/win.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,4 +397,12 @@ impl CarteroWindow {
397397
let imp = self.imp();
398398
imp.finish_create_collection(path)
399399
}
400+
401+
pub fn open_endpoint(&self, path: &str) -> Result<(), CarteroError> {
402+
let contents = crate::file::read_file(&PathBuf::from(path))?;
403+
let endpoint = crate::file::parse_toml(&contents)?;
404+
let imp = self.imp();
405+
imp.add_new_endpoint(Some(endpoint));
406+
Ok(())
407+
}
400408
}

0 commit comments

Comments
 (0)