Browse Source

Cleanup of directories.*, fix of broken PluginApi from one of last commits.

merge-requests/365/head
eidheim 11 years ago
parent
commit
6ba2ae44e2
  1. 4
      juci/config.cc
  2. 147
      juci/directories.cc
  3. 83
      juci/directories.h
  4. 37
      juci/window.cc
  5. 6
      juci/window.h

4
juci/config.cc

@ -81,8 +81,8 @@ void MainConfig::GenerateDirectoryFilter() {
boost::property_tree::ptree ignore_json = dir_json.get_child("ignore");
boost::property_tree::ptree except_json = dir_json.get_child("exceptions");
for ( auto &i : except_json )
dir_cfg->AddException(i.second.get_value<std::string>());
dir_cfg->exceptions.emplace_back(i.second.get_value<std::string>());
for ( auto &i : ignore_json )
dir_cfg->AddIgnore(i.second.get_value<std::string>());
dir_cfg->ignored.emplace_back(i.second.get_value<std::string>());
DEBUG("Directory filter fetched");
}

147
juci/directories.cc

@ -1,100 +1,107 @@
#include "directories.h"
#include "logging.h"
#include "singletons.h"
#include <algorithm>
#include <fstream>
#include "boost/algorithm/string.hpp"
Directories::Controller::Controller() {
namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
Directories::Directories() {
DEBUG("adding treeview to scrolledwindow");
m_ScrolledWindow.add(m_TreeView);
m_ScrolledWindow.set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
add(tree_view);
set_policy(Gtk::POLICY_AUTOMATIC, Gtk::POLICY_AUTOMATIC);
tree_store = Gtk::TreeStore::create(column_record);
tree_view.set_model(tree_store);
tree_view.append_column("", column_record.name);
tree_store->set_sort_column(0, Gtk::SortType::SORT_ASCENDING);
tree_view.signal_row_activated().connect([this](const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column){
INFO("Directory navigation");
auto iter = tree_store->get_iter(path);
if (iter) {
Gtk::TreeModel::Row row = *iter;
std::string upath = Glib::ustring(row[column_record.path]);
boost::filesystem::path fs_path(upath);
if (boost::filesystem::is_directory(fs_path)) {
tree_view.row_expanded(path) ? tree_view.collapse_row(path) : tree_view.expand_row(path, false);
} else {
std::stringstream sstm;
sstm << row[column_record.path];
if(on_row_activated)
on_row_activated(sstm.str());
}
}
});
}
void Directories::Controller::
open_folder(const boost::filesystem::path& dir_path) {
void Directories::open_folder(const boost::filesystem::path& dir_path) {
INFO("Open folder");
m_refTreeModel = Gtk::TreeStore::create(view());
m_TreeView.set_model(m_refTreeModel);
m_TreeView.remove_all_columns();
DEBUG("Getting project name from CMakeLists.txt");
std::string project_name = GetCmakeVarValue(dir_path, "project");
m_TreeView.append_column(project_name, view().m_col_name);
int row_id = 0;
Gtk::TreeModel::Row row;
DEBUG("Listing directories");
list_dirs(dir_path, row, row_id);
DEBUG("Sorting directories");
m_refTreeModel->set_sort_column(0, Gtk::SortType::SORT_ASCENDING);
tree_store->clear();
tree_view.get_column(0)->set_title(get_cmakelists_variable(dir_path, "project"));
add_paths(dir_path, Gtk::TreeModel::Row(), 0);
DEBUG("Folder opened");
}
bool Directories::Controller::IsIgnored(std::string path) {
bool Directories::ignored(std::string path) {
DEBUG("Checking if file-/directory is filtered");
std::transform(path.begin(), path.end(), path.begin(), ::tolower);
if (Singleton::Config::directories()->IsException(path)) {
return false;
for(std::string &i : Singleton::Config::directories()->exceptions) {
if(i == path)
return false;
}
if (Singleton::Config::directories()->IsIgnored(path)) {
return true;
for(auto &i : Singleton::Config::directories()->ignored) {
if(path.find(i, 0) != std::string::npos)
return true;
}
return false;
}
void Directories::Controller::
list_dirs(const boost::filesystem::path& dir_path,
Gtk::TreeModel::Row &parent,
unsigned row_id) {
void Directories::add_paths(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &parent, unsigned row_id) {
boost::filesystem::directory_iterator end_itr;
Gtk::TreeModel::Row child;
Gtk::TreeModel::Row row;
DEBUG("");
// Fill the treeview
for ( boost::filesystem::directory_iterator itr( dir_path );
itr != end_itr;
++itr ) {
if (!IsIgnored(itr->path().filename().string())) {
for(boost::filesystem::directory_iterator itr(dir_path);itr != end_itr;++itr) {
if (!ignored(itr->path().filename().string())) {
if (boost::filesystem::is_directory(itr->status())) {
if (count(itr->path().string()) > count(dir_path.string())) { // is child
child = *(m_refTreeModel->append(parent.children()));
if (boost::filesystem::canonical(itr->path()) > boost::filesystem::canonical(dir_path)) { // is child
child = *(tree_store->append(parent.children()));
std::string col_id("a"+itr->path().filename().string());
child[view().m_col_id] = col_id;
child[view().m_col_name] = itr->path().filename().string();
child[view().m_col_path] = itr->path().string();
list_dirs(itr->path(), child, row_id);
child[column_record.id] = col_id;
child[column_record.name] = itr->path().filename().string();
child[column_record.path] = itr->path().string();
add_paths(itr->path(), child, row_id);
} else {
row = *(m_refTreeModel->append());
row = *(tree_store->append());
std::string col_id("a"+itr->path().filename().string());
row[view().m_col_path] = itr->path().string();
row[view().m_col_id] = col_id;
row[view().m_col_name] = itr->path().filename().string();
list_dirs(itr->path(), parent, row_id);
row[column_record.path] = itr->path().string();
row[column_record.id] = col_id;
row[column_record.name] = itr->path().filename().string();
add_paths(itr->path(), parent, row_id);
}
} else { // is a file
child = *(m_refTreeModel->append(parent.children()));
child = *(tree_store->append(parent.children()));
std::string col_id("b"+itr->path().filename().string());
child[view().m_col_id] = col_id;
child[view().m_col_name] = itr->path().filename().string();
child[view().m_col_path] = itr->path().string();
child[column_record.id] = col_id;
child[column_record.name] = itr->path().filename().string();
child[column_record.path] = itr->path().string();
}
}
}
}
int Directories::Controller::count(const std::string path) {
int count = 0;
for (size_t i = 0; i < path.size(); i++)
if (path[i] == '/')
count++;
return count;
}
std::string Directories::Controller::
GetCmakeVarValue(const boost::filesystem::path& dir_path, std::string command_name) {
std::string Directories::get_cmakelists_variable(const boost::filesystem::path& dir_path, std::string command_name) {
INFO("fetches cmake variable value for: "+command_name);
std::string project_name;
std::string project_name_var;
boost::filesystem::directory_iterator end_itr;
for (boost::filesystem::directory_iterator itr( dir_path );
itr != end_itr;
++itr ) {
for (boost::filesystem::directory_iterator itr( dir_path );itr != end_itr;++itr ) {
if (itr->path().filename().string() == "CMakeLists.txt") {
std::ifstream ifs(itr->path().string());
std::string line;
@ -155,25 +162,3 @@ GetCmakeVarValue(const boost::filesystem::path& dir_path, std::string command_na
INFO("Couldn't find value in CMakeLists.txt");
return "no project name";
}
void Directories::Config::AddIgnore(std::string filter) {
ignore_list_.push_back(filter);
}
void Directories::Config::AddException(std::string filter) {
exception_list_.push_back(filter);
}
bool Directories::Config::IsIgnored(std::string str) {
for ( auto &i : ignore_list() )
if (str.find(i, 0) != std::string::npos)
return true;
return false;
}
bool Directories::Config::IsException(std::string str) {
for ( std::string &i : exception_list() )
if (i == str)
return true;
return false;
}

83
juci/directories.h

@ -2,69 +2,42 @@
#define JUCI_DIRECTORIES_H_
#include <gtkmm.h>
#include <glib.h>
#include <vector>
#include <string>
#include "boost/filesystem.hpp"
#include "boost/algorithm/string.hpp"
#include <utility>
#include <algorithm>
#include <iostream>
#include <fstream>
namespace Directories {
class Directories : public Gtk::ScrolledWindow {
public:
class Config {
public:
std::vector<std::string> ignore_list() { return ignore_list_; }
std::vector<std::string> exception_list() { return exception_list_; }
void AddIgnore(std::string filter);
void AddException(std::string filter);
bool IsException(std::string path);
bool IsIgnored(std::string path);
private:
std::vector<std::string> ignore_list_;
std::vector<std::string> exception_list_;
std::vector<std::string> ignored;
std::vector<std::string> exceptions;
};
class View : public Gtk::TreeModel::ColumnRecord {
class ColumnRecord : public Gtk::TreeModel::ColumnRecord {
public:
View() {
add(m_col_id);
add(m_col_name);
add(m_col_path);
ColumnRecord() {
add(id);
add(name);
add(path);
}
Gtk::TreeModelColumn<Glib::ustring> m_col_id;
Gtk::TreeModelColumn<Glib::ustring> m_col_name;
Gtk::TreeModelColumn<Glib::ustring> m_col_path;
Gtk::TreeModelColumn<Glib::ustring> id;
Gtk::TreeModelColumn<Glib::ustring> name;
Gtk::TreeModelColumn<Glib::ustring> path;
};
class Model { };
class Controller {
public:
Controller();
View& view() { return view_;}
Model& model() { return model_;}
Gtk::ScrolledWindow& widget() {return m_ScrolledWindow;}
void open_folder(const boost::filesystem::path& dir_path);
void list_dirs(const boost::filesystem::path& dir_path,
Gtk::TreeModel::Row &row, unsigned depth);
std::string GetCmakeVarValue(const boost::filesystem::path& dir_path, std::string command_name);
int count(const std::string path);
// Child widgets:
Gtk::Box m_VBox;
Gtk::ScrolledWindow m_ScrolledWindow;
Gtk::TreeView m_TreeView;
Glib::RefPtr<Gtk::TreeStore> m_refTreeModel;
bool IsIgnored(std::string path);
private:
View view_;
Model model_;
protected:
void on_treeview_row_activated(const Gtk::TreeModel::Path& path,
Gtk::TreeViewColumn* column);
};
} // namespace Directories
Directories();
void open_folder(const boost::filesystem::path& dir_path);
std::string get_cmakelists_variable(const boost::filesystem::path& dir_path, std::string command_name);
std::function<void(const std::string &file)> on_row_activated;
private:
void add_paths(const boost::filesystem::path& dir_path, const Gtk::TreeModel::Row &row, unsigned depth);
Gtk::TreeView tree_view;
Glib::RefPtr<Gtk::TreeStore> tree_store;
ColumnRecord column_record;
bool ignored(std::string path);
};
#endif // JUCI_DIRECTORIES_H_

37
juci/window.cc

@ -2,26 +2,26 @@
#include "logging.h"
#include "singletons.h"
#include "config.h"
#include "api.h"
namespace sigc {
SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE
}
Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTICAL) {
Window::Window() : box(Gtk::ORIENTATION_VERTICAL) {
INFO("Create Window");
set_title("juCi++");
set_default_size(600, 400);
set_events(Gdk::POINTER_MOTION_MASK|Gdk::FOCUS_CHANGE_MASK|Gdk::SCROLL_MASK);
add(box);
//TODO: see TODO Window::on_directory_navigation
directories.m_TreeView.signal_row_activated().connect(sigc::mem_fun(*this, &Window::on_directory_navigation));
MainConfig(); //Read the configs here
PluginApi(&this->notebook); //Initialise plugins
add_menu();
box.pack_start(entry_box, Gtk::PACK_SHRINK);
directory_and_notebook_panes.pack1(directories.widget(), true, true); //TODO: should be pack1(directories, ...) Clean up directories.*
directory_and_notebook_panes.pack1(directories, true, true); //TODO: should be pack1(directories, ...) Clean up directories.*
directory_and_notebook_panes.pack2(notebook);
directory_and_notebook_panes.set_position(120);
@ -31,6 +31,10 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
box.pack_end(vpaned);
show_all_children();
directories.on_row_activated=[this](const std::string &file) {
notebook.open(file);
};
entry_box.signal_show().connect([this](){
std::vector<Gtk::Widget*> focus_chain;
focus_chain.emplace_back(&entry_box);
@ -44,6 +48,7 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
notebook.get_current_view()->grab_focus();
}
});
notebook.signal_switch_page().connect([this](Gtk::Widget* page, guint page_num) {
if(search_entry_shown && entry_box.labels.size()>0 && notebook.get_current_page()!=-1) {
notebook.get_current_view()->update_search_occurrences=[this](int number){
@ -63,7 +68,6 @@ Window::Window() : notebook(), plugin_api(&notebook), box(Gtk::ORIENTATION_VERTI
menu_item->set_sensitive((bool)notebook.get_current_view()->rename_similar_tokens);
}
});
INFO("Window created");
} // Window constructor
@ -172,7 +176,7 @@ void Window::add_menu() {
Singleton::terminal()->SetFolderCommand(path);
}
Singleton::terminal()->Compile();
std::string executable = directories.GetCmakeVarValue(path,"add_executable");
std::string executable = directories.get_cmakelists_variable(path,"add_executable");
Singleton::terminal()->Run(executable);
running.unlock();
});
@ -380,27 +384,6 @@ void Window::save_file_dialog() {
}
}
//TODO: move most of it to Directories
void Window::on_directory_navigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column) {
INFO("Directory navigation");
Gtk::TreeModel::iterator iter = directories.m_refTreeModel->get_iter(path);
if (iter) {
Gtk::TreeModel::Row row = *iter;
std::string upath = Glib::ustring(row[directories.view().m_col_path]);
boost::filesystem::path fs_path(upath);
if (boost::filesystem::is_directory(fs_path)) {
directories.m_TreeView.row_expanded(path) ?
directories.m_TreeView.collapse_row(path) :
directories.m_TreeView.expand_row(path, false);
} else {
std::stringstream sstm;
sstm << row[directories.view().m_col_path];
std::string file = sstm.str();
notebook.open(file);
}
}
}
void Window::search_and_replace_entry() {
entry_box.clear();
entry_box.labels.emplace_back();

6
juci/window.h

@ -1,16 +1,16 @@
#ifndef JUCI_WINDOW_H_
#define JUCI_WINDOW_H_
#include "api.h"
#include <cstddef>
#include "directories.h"
#include "entrybox.h"
#include "notebook.h"
class Window : public Gtk::Window {
public:
Window();
Notebook notebook;
Directories::Controller directories;
Directories directories;
protected:
bool on_key_press_event(GdkEventKey *event);
bool on_delete_event (GdkEventAny *event);
@ -19,7 +19,6 @@ private:
Gtk::VPaned vpaned;
Gtk::Paned directory_and_notebook_panes;
EntryBox entry_box;
PluginApi plugin_api;
std::mutex running;
void add_menu();
@ -28,7 +27,6 @@ private:
void open_folder_dialog();
void open_file_dialog();
void save_file_dialog();
void on_directory_navigation(const Gtk::TreeModel::Path& path, Gtk::TreeViewColumn* column);
void search_and_replace_entry();
void rename_token_entry();

Loading…
Cancel
Save