forked from blackhole89/notekit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnavigation.cpp
414 lines (349 loc) · 12.1 KB
/
navigation.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
#include "navigation.h"
#include "mainwindow.h"
#include <giomm/file.h>
//extern CMainWindow *mainwindow;
CNavigationView::Columns::Columns() : Gtk::TreeModel::ColumnRecord()
{
add(name);
add(full_path);
add(ext);
add(ord);
add(type);
add(expanded);
}
CNavigationView::CNavigationView()
{
store=Gtk::TreeStore::create(cols);
/* set up sorting */
store->set_sort_func(cols.name,[this] (const Gtk::TreeModel::iterator& a, const Gtk::TreeModel::iterator& b) {
if((*b)[cols.type]==CT_ADDER) return -1;
else if((*a)[cols.type]==CT_ADDER) return 1;
else {
Glib::ustring namea, nameb;
if(((Glib::ustring)(*a)[cols.ord]).size()) namea = (*a)[cols.ord]; else namea= (*a)[cols.name];
if(((Glib::ustring)(*b)[cols.ord]).size()) nameb = (*b)[cols.ord]; else nameb= (*b)[cols.name];
return namea<nameb?-1:1;
}
});
store->set_sort_column(cols.name,Gtk::SORT_ASCENDING);
}
void CNavigationView::SetBasePath(std::string newbase)
{
base=newbase;
}
void CNavigationView::on_row_edit_start(Gtk::CellEditable* cell_editable, const Glib::ustring& path)
{
auto pEntry = dynamic_cast<Gtk::Entry*>(cell_editable);
if(pEntry) {
if(pEntry->get_text() == "+")
pEntry->set_text("");
}
}
void CNavigationView::on_row_edit_commit(const Glib::ustring& path_string, const Glib::ustring& new_text)
{
Gtk::TreePath path(path_string);
std::string cmd;
if(auto row = store->get_iter(path)) {
switch( (*row)[cols.type] ) {
case CT_FILE:
case CT_DIR_LOADED:
case CT_DIR_UNLOADED:
cmd = "mv \"";
cmd += base;
cmd += (Glib::ustring)(*row)[cols.full_path];
cmd += (Glib::ustring)(*row)[cols.name];
cmd += (Glib::ustring)(*row)[cols.ext];
cmd += "\" \"";
cmd += base;
cmd += (Glib::ustring)(*row)[cols.full_path];
cmd += new_text;
cmd += (Glib::ustring)(*row)[cols.ext];
cmd += "\"";
if(!system(cmd.c_str())) {
std::string oldname = Row2Path(row);
(*row)[cols.name] = new_text;
std::string newname = Row2Path(row);
if( (*row)[cols.type] == CT_DIR_LOADED || (*row)[cols.type] == CT_DIR_UNLOADED ) {
FixPaths( (Glib::ustring)(*row)[cols.full_path] + (Glib::ustring)(*row)[cols.name], &row->children() );
} else {
HandleRename( oldname, newname );
}
}
break;
case CT_ADDER:
if(new_text[new_text.length()-1]=='/') {
cmd = "mkdir \"";
cmd += base;
cmd += (Glib::ustring)(*row)[cols.full_path];
cmd += new_text;
cmd += "\"";
if(!system(cmd.c_str())) {
auto newrow = store->insert(row);
(*newrow)[cols.name] = new_text.substr(0,new_text.size()-1);
(*newrow)[cols.full_path] = (Glib::ustring)(*row)[cols.full_path];
(*newrow)[cols.ext] = "";
(*newrow)[cols.type] = CT_DIR_LOADED;
CreateAdder(newrow);
v->expand_row(store->get_path(newrow),false);
v->set_cursor(store->get_path(newrow));
}
} else {
cmd = "touch \"";
cmd += base;
cmd += (Glib::ustring)(*row)[cols.full_path];
cmd += new_text;
cmd += ".md\"";
if(!system(cmd.c_str())) {
auto newrow = store->insert(row);
(*newrow)[cols.name] = new_text;
(*newrow)[cols.full_path] = (Glib::ustring)(*row)[cols.full_path];
(*newrow)[cols.ext] = ".md";
(*newrow)[cols.type] = CT_FILE;
v->set_cursor(store->get_path(newrow));
mainwindow->OpenDocument(Row2Path(newrow));
}
}
break;
}
}
}
std::string CNavigationView::Row2Path(Gtk::TreeModel::iterator row)
{
std::string fname;
fname += (Glib::ustring)(*row)[cols.full_path];
fname += (Glib::ustring)(*row)[cols.name];
fname += (Glib::ustring)(*row)[cols.ext];
return fname;
}
void CNavigationView::on_row_activated(const Gtk::TreeModel::Path &path, const Gtk::TreeViewColumn *col)
{
printf("activate row\n");
auto row = store->get_iter(path);
if((*row)[cols.type]!=CT_FILE) return;
std::string fname = Row2Path(row);
mainwindow->OpenDocument(fname);
}
#ifdef XATTRS
#include <sys/xattr.h>
#endif
bool CNavigationView::on_expand_row(const Gtk::TreeModel::iterator& iter, const Gtk::TreeModel::Path& path)
{
if( (*iter)[cols.type] == CT_DIR_UNLOADED )
ExpandDirectory( (Glib::ustring)(*iter)[cols.full_path] + (Glib::ustring)(*iter)[cols.name], &iter->children() );
(*iter)[cols.type] = CT_DIR_LOADED;
#ifdef XATTRS
int attr='1';
setxattr((base+Row2Path(iter)).c_str(),"user.nkexpand",&attr,1,0);
#else
Glib::RefPtr<Gio::File> f = Gio::File::create_for_path(base+Row2Path(iter));
f->set_attribute_string("xattr::nkexpand","1",Gio::FILE_QUERY_INFO_NONE);
#endif
(*iter)[cols.expanded]=1;
return false;
}
void CNavigationView::on_postexpand_row(const Gtk::TreeModel::iterator& iter, const Gtk::TreeModel::Path& path)
{
for(auto r : iter->children()) {
if( r[cols.expanded] ) {
v->expand_row(store->get_path(r),false);
}
Glib::ustring name = r[cols.full_path]+r[cols.name]+r[cols.ext];
if( name==mainwindow->selected_document ) {
v->get_selection()->select( r );
}
}
}
void CNavigationView::on_collapse_row(const Gtk::TreeModel::iterator& iter, const Gtk::TreeModel::Path& path)
{
/* not unloading the row for now */
#ifdef XATTRS
int attr='0';
setxattr((base+Row2Path(iter)).c_str(),"user.nkexpand",&attr,0,0);
#else
Glib::RefPtr<Gio::File> f = Gio::File::create_for_path(base+Row2Path(iter));
printf("%d\n",f->set_attribute_string("xattr::nkexpand","",Gio::FILE_QUERY_INFO_NONE));
#endif
(*iter)[cols.expanded]=0;
}
void CNavigationView::on_button_press_event(GdkEventButton* event)
{
if( (event->type == GDK_BUTTON_PRESS) && (event->button == 3) )
{
popup.popup_at_pointer((GdkEvent*)event);
}
}
void CNavigationView::AttachView(CMainWindow *w, Gtk::TreeView *view)
{
mainwindow=w;
v=view;
v->get_selection()->set_mode(Gtk::SELECTION_BROWSE);
/*v->get_selection()->set_select_function([this] (const Glib::RefPtr<Gtk::TreeModel>& m, const Gtk::TreeModel::Path& p, bool) {
int type = (*m->get_iter(p))[cols.type];
if(type==CT_DIR_LOADED || type==CT_DIR_UNLOADED) return false;
return true;
}); */ // this stops us from editing the directory name too...
v->set_size_request(200,-1);
v->set_headers_visible(false);
v->set_enable_tree_lines(true);
v->set_model(store);
v->set_activate_on_single_click(true);
//conns[0] = store->signal_row_changed().connect(sigc::mem_fun(*this,&CNavigationView::on_row_edit));
v->append_column("Name",cols.name);
/* set up editing */
Gtk::CellRendererText *cr = (Gtk::CellRendererText*)v->get_column_cell_renderer(0);
cr->property_editable() = true;
conns[0] = cr->signal_editing_started().connect(sigc::mem_fun(this,&CNavigationView::on_row_edit_start));
conns[1] = cr->signal_edited().connect(sigc::mem_fun(this,&CNavigationView::on_row_edit_commit));
conns[2] = v->signal_row_activated().connect(sigc::mem_fun(this,&CNavigationView::on_row_activated));
conns[3] = v->signal_test_expand_row().connect(sigc::mem_fun(this,&CNavigationView::on_expand_row));
conns[4] = v->signal_row_expanded().connect(sigc::mem_fun(this,&CNavigationView::on_postexpand_row));
conns[5] = v->signal_row_collapsed().connect(sigc::mem_fun(this,&CNavigationView::on_collapse_row));
conns[6] = v->signal_button_press_event().connect_notify(sigc::mem_fun(this,&CNavigationView::on_button_press_event),false);
/* expand root directory */
ExpandDirectory("",NULL);
for(auto r : store->children()) {
if( r[cols.expanded] ) {
v->expand_row(store->get_path(r),false);
}
Glib::ustring name = r[cols.full_path]+r[cols.name]+r[cols.ext];
if( name==mainwindow->selected_document ) {
v->get_selection()->select( r );
}
}
Gtk::TreeModel::iterator adder;
adder = store->append();
(*adder)[cols.name]="+";
(*adder)[cols.full_path]="/";
(*adder)[cols.type]=CT_ADDER;
/* create popup menu */
auto item = new Gtk::MenuItem("_Delete", true);
Gtk::manage(item);
item->signal_activate().connect( sigc::mem_fun(this,&CNavigationView::MaybeDeleteSelected) );
popup.append(*item);
popup.accelerate(*v);
popup.show_all();
}
void CNavigationView::MaybeDeleteSelected()
{
Gtk::TreeIter i = v->get_selection()->get_selected();
switch( (*i)[cols.type] ) {
case CT_FILE: {
Gtk::MessageDialog dialog(*mainwindow, "Delete note "+(*i)[cols.name]+"?",
false /* use_markup */, Gtk::MESSAGE_QUESTION,
Gtk::BUTTONS_OK_CANCEL);
dialog.set_secondary_text("This action can not be undone.");
int result = dialog.run();
//Handle the response:
if(result==Gtk::RESPONSE_OK) {
mainwindow->OpenDocument("");
std::string cmd;
cmd = "rm \"";
cmd += base;
cmd += (Glib::ustring)(*i)[cols.full_path];
cmd += (Glib::ustring)(*i)[cols.name];
cmd += (Glib::ustring)(*i)[cols.ext];
cmd += "\"";
if(!system(cmd.c_str())) {
store->erase(i);
}
}
break;
}
case CT_DIR_LOADED:
case CT_DIR_UNLOADED: {
Gtk::MessageDialog dialog(*mainwindow, "Delete FOLDER "+(*i)[cols.name]+"?",
false /* use_markup */, Gtk::MESSAGE_WARNING,
Gtk::BUTTONS_OK_CANCEL);
dialog.set_secondary_text("This action can not be undone, and many notes may be lost.");
int result = dialog.run();
//Handle the response:
if(result==Gtk::RESPONSE_OK) {
mainwindow->OpenDocument("");
std::string cmd;
cmd = "rm -r \"";
cmd += base;
cmd += (Glib::ustring)(*i)[cols.full_path];
cmd += (Glib::ustring)(*i)[cols.name];
cmd += (Glib::ustring)(*i)[cols.ext];
cmd += "\"";
if(!system(cmd.c_str())) {
store->erase(i);
}
}
break;
}
}
}
void CNavigationView::HandleRename(std::string oldname, std::string newname)
{
//printf("handlerename %s->%s: %s\n",oldname.c_str(),newname.c_str(),mainwindow->active_document.c_str() );
if(mainwindow->active_document == oldname)
mainwindow->active_document = newname;
}
void CNavigationView::FixPaths(std::string path, const Gtk::TreeNodeChildren *node)
{
for(auto &a: *node) {
std::string oldname = Row2Path(a);
a[cols.full_path] = path+"/";
HandleRename( oldname, Row2Path(a) ); // change active filename in main window if necessary
if(a[cols.type] == CT_DIR_LOADED || a[cols.type] == CT_DIR_UNLOADED) {
FixPaths(path+"/"+a[cols.name], &a.children());
}
}
}
void CNavigationView::CreateAdder(Gtk::TreeModel::iterator r)
{
Gtk::TreeModel::iterator adder;
adder = store->append(r->children());
(*adder)[cols.name]="+";
(*adder)[cols.full_path]=(*r)[cols.full_path]+(*r)[cols.name]+"/";
(*adder)[cols.type]=CT_ADDER;
}
void CNavigationView::ExpandDirectory(std::string path, const Gtk::TreeNodeChildren *node)
{
try {
Glib::RefPtr<Gio::File> file = Gio::File::create_for_path(base+path);
Glib::RefPtr<Gio::FileEnumerator> child_enumeration = file->enumerate_children("standard::name,standard::type,xattr::nkorder,xattr::nkexpand",Gio::FILE_QUERY_INFO_NONE);
Glib::RefPtr<Gio::FileInfo> file_info;
std::vector<Glib::RefPtr<Gio::FileInfo> > files;
while ((file_info = child_enumeration->next_file())) files.push_back(file_info);
/*std::sort(files.begin(), files.end(), [](Glib::RefPtr<Gio::FileInfo> &a, Glib::RefPtr<Gio::FileInfo> &b) {
return (a->get_name() > b->get_name()); } ); */
for( auto file_info : files )
{
std::string fname = file_info->get_name();
Glib::ustring order = file_info->get_attribute_as_string("xattr::nkorder");
bool is_dir = file_info->get_file_type()==Gio::FILE_TYPE_DIRECTORY;
Gtk::TreeModel::iterator r;
if(is_dir) {
if(node) r=store->prepend(*node);
else r=store->prepend();
(*r)[cols.name] = fname;
(*r)[cols.full_path] = path+"/";
(*r)[cols.ext] = "";
(*r)[cols.ord] = order;
(*r)[cols.type] = CT_DIR_UNLOADED;
CreateAdder(r);
/* WARN: this needs the view to already be attached */
if(file_info->get_attribute_string("xattr::nkexpand").size()) {
(*r)[cols.expanded] = 1;
} else {
(*r)[cols.expanded] = 0;
}
} else {
size_t pos;
if((pos = fname.find(".md"))!=std::string::npos) {
if(node) r=store->prepend(*node);
else r=store->prepend();
(*r)[cols.name] = fname.substr(0,pos);
(*r)[cols.full_path] = path+"/";
(*r)[cols.ext] = ".md";
(*r)[cols.ord] = order;
(*r)[cols.type] = CT_FILE;
}
}
}
} catch(Gio::Error &e) {
printf("Gio error when expanding directory: %s\n",e.what().c_str());
}
}