Skip to content

Commit

Permalink
List and open requests inside collections
Browse files Browse the repository at this point in the history
  • Loading branch information
danirod committed Jun 26, 2024
1 parent 460f121 commit 501419d
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 14 deletions.
17 changes: 10 additions & 7 deletions data/ui/main_window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@ template $CarteroWindow: Adw.ApplicationWindow {
$CarteroSidebar collections {}
};

content: Gtk.Box {
orientation: vertical;
content: Adw.ToolbarView {
[top]
Gtk.Box {
orientation: vertical;

Adw.HeaderBar {
[title]
Label main_label {}
}
Adw.HeaderBar {
[title]
Label main_label {}
}

Adw.ToastOverlay toaster {
Adw.TabBar tabs {
view: tabview;
}
}

Adw.ToastOverlay toaster {
Adw.TabView tabview {}
}
};
Expand Down
48 changes: 47 additions & 1 deletion src/fs/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
//
// SPDX-License-Identifier: GPL-3.0-or-later

use std::path::Path;
use std::fs::DirEntry;
use std::path::{Path, PathBuf};
use std::{fs::File, io::Write};

use crate::{error::CarteroError, objects::Collection};
Expand Down Expand Up @@ -45,3 +46,48 @@ pub fn open_collection(path: &Path) -> Result<Collection, CarteroError> {
let metadata: Metadata = toml::from_str(&metadata_content)?;
Ok(metadata.into())
}

fn is_cartero_request(entry: &Result<DirEntry, std::io::Error>) -> bool {
let Ok(entry) = entry else {
return false;
};
let path = entry.path();
if !path.is_file() {
return false;
}
let Some(ext) = path.as_path().extension() else {
return false;
};
ext.to_str().is_some_and(|s| s == "cartero")
}

fn is_cartero_folder(entry: &Result<DirEntry, std::io::Error>) -> bool {
let Ok(entry) = entry else {
return false;
};
let path = entry.path();
if !path.is_dir() {
return false;
}

let metadata = path.join("cartero-metadata");
metadata.exists() && metadata.is_file()
}

pub fn list_folders(path: &Path) -> Result<Vec<PathBuf>, CarteroError> {
let folders = path
.read_dir()?
.filter(is_cartero_folder)
.map(|entry| entry.unwrap().path())
.collect();
Ok(folders)
}

pub fn list_endpoints(path: &Path) -> Result<Vec<PathBuf>, CarteroError> {
let endpoints = path
.read_dir()?
.filter(is_cartero_request)
.map(|entry| entry.unwrap().path())
.collect();
Ok(endpoints)
}
61 changes: 55 additions & 6 deletions src/widgets/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,22 @@ use crate::{
};

mod imp {
use std::path::PathBuf;

use adw::subclass::bin::BinImpl;
use glib::subclass::InitializingObject;
use glib::Object;
use gtk::gio::{ListModel, ListStore};
use gtk::subclass::prelude::*;
use gtk::{
prelude::*, CompositeTemplate, ListView, SignalListItemFactory, SingleSelection,
TreeExpander, TreeListModel,
TreeExpander, TreeListModel, TreeListRow,
};

use crate::objects::{KeyValueItem, TreeNode, TreeNodeKind};
use crate::fs::collection::{list_endpoints, list_folders};
use crate::objects::{TreeNode, TreeNodeKind};
use crate::widgets::sidebar_row::SidebarRow;
use crate::win::CarteroWindow;

#[derive(CompositeTemplate, Default)]
#[template(resource = "/es/danirod/Cartero/sidebar.ui")]
Expand Down Expand Up @@ -81,10 +85,41 @@ mod imp {
let root_model: ListStore = Object::builder()
.property("item-type", TreeNode::static_type())
.build();
TreeListModel::new(root_model, false, false, |_obj: &Object| {
TreeListModel::new(root_model, false, false, |obj: &Object| {
let tree_node = obj.downcast_ref::<TreeNode>()?;

if tree_node.node_type() == TreeNodeKind::Endpoint {
return None;
}

let path_buf = PathBuf::from(tree_node.path());
let folders = list_folders(&path_buf);
let child = list_endpoints(&path_buf);
println!("{:?}", child);

let children: ListStore = Object::builder()
.property("item-type", KeyValueItem::static_type())
.property("item-type", TreeNode::static_type())
.build();

if let Ok(folders) = folders {
for f in folders {
let node = TreeNode::default();
node.set_path(f.to_str().unwrap());
node.set_title(f.file_name().unwrap().to_str().unwrap());
node.set_node_type(TreeNodeKind::Folder);
children.append(&node);
}
}
if let Ok(child) = child {
for c in child {
let node = TreeNode::default();
node.set_path(c.to_str().unwrap());
node.set_title(c.file_name().unwrap().to_str().unwrap());
node.set_node_type(TreeNodeKind::Endpoint);
children.append(&node);
}
}

let model = children.upcast::<ListModel>();
Some(model)
})
Expand All @@ -100,8 +135,22 @@ mod imp {

#[template_callback]
fn on_activate(list: ListView, pos: u32, data: &Object) {
println!("activate()");
println!("list = {:?} \n pos = {:?} \n data = {:?}", list, pos, data);
let window = list.root().and_downcast::<CarteroWindow>().unwrap();
let Some(model) = list.model() else {
return;
};

let row = model.item(pos).and_downcast::<TreeListRow>().unwrap();
let inner_value = row.item().and_downcast::<TreeNode>().unwrap();

let path = inner_value.path();

match inner_value.node_type() {
TreeNodeKind::Endpoint => {
window.open_endpoint(&path);
}
_ => println!("Not implemented yet, wait a minute"),
}
}

#[template_callback]
Expand Down
8 changes: 8 additions & 0 deletions src/win.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,12 @@ impl CarteroWindow {
let imp = self.imp();
imp.finish_create_collection(path)
}

pub fn open_endpoint(&self, path: &str) -> Result<(), CarteroError> {
let contents = crate::file::read_file(&PathBuf::from(path))?;
let endpoint = crate::file::parse_toml(&contents)?;
let imp = self.imp();
imp.add_new_endpoint(Some(endpoint));
Ok(())
}
}

0 comments on commit 501419d

Please sign in to comment.