The Birdfont Source Code


All Repositories / birdfont.git / blob – RSS feed

FileDialogTab.vala in libbirdfont

This file is a part of the Birdfont project.

Contributing

Send patches or pull requests to johan.mattsson.m@gmail.com.
Clone this repository: git clone https://github.com/johanmattssonm/birdfont.git

Revisions

View the latest version of libbirdfont/FileDialogTab.vala.
Debug info for file utility
1 /* 2 Copyright (C) 2014 2015 Johan Mattsson 3 4 This library is free software; you can redistribute it and/or modify 5 it under the terms of the GNU Lesser General Public License as 6 published by the Free Software Foundation; either version 3 of the 7 License, or (at your option) any later version. 8 9 This library is distributed in the hope that it will be useful, but 10 WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 Lesser General Public License for more details. 13 */ 14 using Cairo; 15 using Math; 16 17 namespace BirdFont { 18 19 public class FileDialogTab : Table { 20 Gee.ArrayList<Row> rows; 21 22 const int DIRECTORY = -3; 23 const int FILE = -2; 24 25 Gee.ArrayList<string> files; 26 Gee.ArrayList<string> directories; 27 28 string title; 29 FileChooser action; 30 31 File current_dir; 32 33 string selected_filename; 34 TextListener listener; 35 36 private static bool has_drive_letters = false; 37 private static Gee.ArrayList<string> drive_letters; 38 39 bool select_folder = false; 40 41 #if LINUX 42 public static const string path_separator = "/"; 43 #else 44 public static const string path_separator = "\\"; 45 #endif 46 47 public FileDialogTab (string title, FileChooser action, bool folder) { 48 this.title = title; 49 this.action = action; 50 51 select_folder = folder; 52 rows = new Gee.ArrayList<Row> (); 53 files = new Gee.ArrayList<string> (); 54 directories = new Gee.ArrayList<string> (); 55 56 selected_filename = ""; 57 selected_canvas (); 58 } 59 60 public static void add_drive_letter (char c) { 61 if (!has_drive_letters) { 62 drive_letters = new Gee.ArrayList<string> (); 63 has_drive_letters = true; 64 } 65 66 drive_letters.add (@"$((!) c.to_string ()):\\"); 67 } 68 69 public override void update_rows () { 70 Row row; 71 72 rows.clear (); 73 74 // add empty rows under the text area 75 row = new Row.headline (""); 76 rows.add (row); 77 78 if (select_folder) { 79 row = new Row.headline (t_("Select a Folder")); 80 } 81 82 if (directories.size > 0) { 83 row = new Row.headline (t_("Folders")); 84 rows.add (row); 85 } 86 87 foreach (string dir in directories) { 88 row = new Row.columns_1 (dir, DIRECTORY, false); 89 row.set_row_data (new SelectedFile (dir)); 90 rows.add (row); 91 } 92 93 if (files.size > 0) { 94 row = new Row.headline (t_("Files")); 95 rows.add (row); 96 } 97 98 foreach (string f in files) { 99 row = new Row.columns_1 (f, FILE, false); 100 row.set_row_data (new SelectedFile (f)); 101 rows.add (row); 102 } 103 104 GlyphCanvas.redraw (); 105 } 106 107 public override Gee.ArrayList<Row> get_rows () { 108 return rows; 109 } 110 111 public override void selected_row (Row row, int column, bool delete_button) { 112 SelectedFile f; 113 114 if (row.get_index () == FILE) { 115 if (!select_folder) { 116 return_if_fail (row.get_row_data () is SelectedFile); 117 f = (SelectedFile) row.get_row_data (); 118 selected_filename = f.file_name; 119 } 120 } else if (row.get_index () == DIRECTORY) { 121 return_if_fail (row.get_row_data () is SelectedFile); 122 f = (SelectedFile) row.get_row_data (); 123 124 if (select_folder && f.file_name != "..") { 125 selected_filename = f.file_name; 126 } else { 127 if (f.file_name.index_of (":") > -1) { 128 propagate_files (f.file_name); 129 } else { 130 printd (@"Subdir: $(f.file_name) in $((!) current_dir.get_path ()) path_separator: $path_separator\n"); 131 propagate_files (((!) current_dir.get_path ()) + path_separator + f.file_name); 132 } 133 134 selected_filename = ""; 135 } 136 } 137 138 show_text_area (selected_filename); 139 } 140 141 public override void selected_canvas () { 142 string d; 143 show_text_area (""); 144 145 d = Preferences.get ("file_dialog_dir"); 146 147 if (d == "") { 148 d = Environment.get_home_dir (); 149 } 150 151 propagate_files (d); 152 base.selected_canvas (); 153 } 154 155 public void propagate_files (string dir) { 156 FileEnumerator enumerator; 157 FileInfo? file_info; 158 string fn; 159 160 printd (@"Propagate_files in directory: $dir\n"); 161 162 files.clear (); 163 directories.clear (); 164 165 current_dir = File.new_for_path (dir); 166 167 Preferences.set ("file_dialog_dir", dir); 168 169 if (current_dir.get_parent () != null) { 170 directories.add (".."); 171 } 172 173 try { 174 enumerator = current_dir.enumerate_children (FileAttribute.STANDARD_NAME + "," + FileAttribute.STANDARD_TYPE, 0); 175 176 while ((file_info = enumerator.next_file ()) != null) { 177 fn = ((!)file_info).get_name (); 178 if (!fn.has_prefix (".")) { 179 if (((!)file_info).get_file_type () == FileType.DIRECTORY) { 180 directories.add (fn); 181 } else { 182 files.add (fn); 183 } 184 } 185 } 186 } catch (Error e) { 187 warning (e.message); 188 } 189 190 directories.sort (); 191 192 if (has_drive_letters) { 193 for (int i = drive_letters.size - 1; i >= 0; i--) { 194 directories.insert (0, drive_letters.get (i)); 195 } 196 } 197 198 files.sort (); 199 200 layout (); 201 base.selected_canvas (); 202 scroll_to (0); 203 } 204 205 public void show_text_area (string text) { 206 listener = new TextListener ("", text, this.title); 207 208 listener.signal_text_input.connect ((text) => { 209 selected_filename = text; 210 }); 211 212 listener.signal_submit.connect (() => { 213 File f; 214 215 MainWindow.get_tab_bar ().close_display (this); 216 217 if (selected_filename == "") { 218 action.cancel (); 219 } else { 220 f = get_child (current_dir, selected_filename); 221 action.file_selected ((!)f.get_path ()); 222 } 223 }); 224 225 TabContent.show_text_input (listener); 226 } 227 228 public override void double_click (uint button, double ex, double ey) { 229 File f; 230 231 button_release ((int) button, ex, ey); 232 233 if (is_null (selected_filename)) { 234 warning ("No file."); 235 return; 236 } 237 238 if (select_folder) { 239 if (selected_filename.index_of (":") > -1) { 240 propagate_files (selected_filename); 241 } else { 242 propagate_files (((!) current_dir.get_path ()) + path_separator + selected_filename); 243 } 244 } else { 245 if (selected_filename != "") { 246 f = get_child (current_dir, selected_filename); 247 action.file_selected ((!) f.get_path ()); 248 } 249 } 250 } 251 252 public override string get_label () { 253 return title; 254 } 255 256 public override string get_name () { 257 return "FileDialogTab"; 258 } 259 260 public override void button_release (int button, double ex, double ey) { 261 base.button_release (button, ex, ey); 262 show_text_area (selected_filename); 263 } 264 265 class SelectedFile : GLib.Object { 266 public string file_name; 267 268 public SelectedFile (string fn) { 269 file_name = fn; 270 } 271 } 272 } 273 274 } 275