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
|
/***
Copyright (c) 1999, 2000 Red Hat, Inc.
Copyright (c) 2000, 2001 Eazel, Inc.
Copyright (c) 2013 Julián Unrrein <junrrein@gmail.com>
Copyright (c) 2015-2017 elementary LLC (http://launchpad.net/elementary)
This program is free software: you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License version 3, as published
by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranties of
MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
Authors: Elliot Lee <sopwith@redhat.com>,
Darin Adler <darin@bentspoon.com>,
Julián Unrrein <junrrein@gmail.com>
***/
private Marlin.Application application_singleton = null;
public class Marlin.Application : Granite.Application {
private VolumeMonitor volume_monitor;
private Marlin.Progress.UIHandler progress_handler;
private Marlin.ClipboardManager clipboard;
private Marlin.Thumbnailer thumbnailer;
private Gtk.RecentManager recent;
private const int MARLIN_ACCEL_MAP_SAVE_DELAY = 15;
private const uint MAX_WINDOWS = 25;
public int window_count { get; private set; }
bool quitting = false;
construct {
/* Needed by Glib.Application */
this.application_id = Marlin.APP_ID; //Ensures an unique instance.
this.flags = ApplicationFlags.HANDLES_COMMAND_LINE;
/* Needed by Granite.Application */
this.program_name = _(Marlin.APP_TITLE);
this.exec_name = APP_NAME;
this.build_version = Config.VERSION;
this.app_copyright = Marlin.COPYRIGHT;
this.app_years = Marlin.APP_YEARS;
this.about_license_type = Gtk.License.GPL_3_0;
this.app_icon = Marlin.ICON_APP_LOGO;
this.main_url = Marlin.LAUNCHPAD_URL;
this.bug_url = Marlin.BUG_URL;
this.help_url = Marlin.HELP_URL;
this.translate_url = Marlin.TRANSLATE_URL;
this.about_authors = Marlin.AUTHORS;
this.about_documenters = { null };
this.about_artists = Marlin.ARTISTS;
this.about_comments = _(Marlin.COMMENTS);
this.about_translators = Marlin.TRANSLATORS;
application_singleton = this;
}
public static new unowned Application get () {
if (application_singleton == null)
application_singleton = new Marlin.Application ();
return application_singleton;
}
public override void startup () {
base.startup ();
if (Granite.Services.Logger.DisplayLevel != Granite.Services.LogLevel.DEBUG)
Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.INFO;
message ("Report any issues/bugs you might find to http://bugs.launchpad.net/pantheon-files");
init_schemas ();
Gtk.IconTheme.get_default ().changed.connect (() => {
Marlin.IconInfo.clear_caches ();
});
progress_handler = new Marlin.Progress.UIHandler (this);
this.clipboard = Marlin.ClipboardManager.get_for_display ();
this.thumbnailer = Marlin.Thumbnailer.get ();
this.recent = new Gtk.RecentManager ();
plugins = new Marlin.PluginManager (Config.PLUGIN_DIR);
/**TODO** move the volume manager here? */
/**TODO** gio: This should be using the UNMOUNTED feature of GFileMonitor instead */
this.volume_monitor = VolumeMonitor.get ();
this.volume_monitor.mount_removed.connect (mount_removed_callback);
#if HAVE_UNITY
QuicklistHandler.get_singleton ();
#endif
window_count = 0;
this.window_added.connect_after (() => {window_count++;});
this.window_removed.connect (() => {
window_count--;
});
}
public unowned Marlin.ClipboardManager get_clipboard_manager () {
return this.clipboard;
}
public unowned Gtk.RecentManager get_recent_manager () {
return this.recent;
}
public override int command_line (ApplicationCommandLine cmd) {
this.hold ();
int result = _command_line (cmd);
this.release ();
return result;
}
/* The array that holds the file commandline arguments
needs some boilerplate so its size gets updated. */
[CCode (array_length = false, array_null_terminated = true)]
private string[]? remaining = null;
private int _command_line (ApplicationCommandLine cmd) {
/* Setup the argument parser */
bool version = false;
bool open_in_tab = false;
bool create_new_window = false;
bool kill_shell = false;
bool debug = false;
OptionEntry[] options = new OptionEntry [7];
options [0] = { "version", '\0', 0, OptionArg.NONE, ref version,
N_("Show the version of the program."), null };
options [1] = { "tab", 't', 0, OptionArg.NONE, ref open_in_tab,
N_("Open uri(s) in new tab"), null };
options [2] = { "new-window", 'n', 0, OptionArg.NONE, out create_new_window,
N_("New Window"), null };
options [3] = { "quit", 'q', 0, OptionArg.NONE, ref kill_shell,
N_("Quit Files."), null };
options [4] = { "debug", 'd', 0, OptionArg.NONE, ref debug,
N_("Enable debug logging"), null };
/* "" = G_OPTION_REMAINING: Catches the remaining arguments */
options [5] = { "", 0, 0, OptionArg.STRING_ARRAY, ref remaining,
null, N_("[URI...]") };
options [6] = { null };
var context = new OptionContext (_("\n\nBrowse the file system with the file manager"));
context.add_main_entries (options, null);
context.add_group (Gtk.get_option_group (true));
string[] args = cmd.get_arguments ();
/* We need to store arguments in an unowned variable for context.parse */
unowned string[] args_aux = args;
/* Parse arguments */
try {
context.parse (ref args_aux);
} catch (OptionError error) {
cmd.printerr ("Could not parse arguments: %s\n", error.message);
return Posix.EXIT_FAILURE;
}
/* Handle arguments */
if (debug)
Granite.Services.Logger.DisplayLevel = Granite.Services.LogLevel.DEBUG;
if (version) {
cmd.print ("pantheon-files %s\n", Config.VERSION);
return Posix.EXIT_SUCCESS;
}
if (kill_shell) {
if (remaining != null) {
cmd.printerr ("%s\n", _("--quit cannot be used with URIs."));
return Posix.EXIT_FAILURE;
} else {
this.quit ();
return Posix.EXIT_SUCCESS;
}
}
File[] files = null;
/* Convert remaining arguments to GFiles */
foreach (string filepath in remaining) {
var file = File.new_for_commandline_arg (filepath);
if (file != null)
files += (file);
}
/* Open application */
if (create_new_window) {
var win = create_window (null);
win.add_tab (); /* Default tab */
} else if (open_in_tab) {
open_tabs (files);
} else {
open_windows (files);
}
return Posix.EXIT_SUCCESS;
}
public override void quit_mainloop () {
warning ("Quitting mainloop");
Marlin.IconInfo.clear_caches ();
base.quit_mainloop ();
}
public new void quit () {
/* Protect against holding Ctrl-Q down */
if (quitting)
return;
quitting = true;
unowned List<Gtk.Window> window_list = this.get_windows ();
window_list.@foreach ((window) => {
((Marlin.View.Window)window).quit ();
});
base.quit ();
}
public void folder_deleted (GLib.File file) {
unowned List<Gtk.Window> window_list = this.get_windows ();
window_list.@foreach ((window) => {
((Marlin.View.Window)window).folder_deleted (file);
});
}
private void mount_removed_callback (VolumeMonitor monitor, Mount mount) {
/* Notify each window */
foreach (var window in this.get_windows ()) {
((Marlin.View.Window)window).mount_removed (mount);
}
}
private void init_schemas () {
/* GSettings parameters */
Preferences.settings = new Settings ("org.pantheon.files.preferences");
Preferences.marlin_icon_view_settings = new Settings ("org.pantheon.files.icon-view");
Preferences.marlin_list_view_settings = new Settings ("org.pantheon.files.list-view");
Preferences.marlin_column_view_settings = new Settings ("org.pantheon.files.column-view");
/* Bind settings with GOFPreferences */
Preferences.settings.bind ("show-hiddenfiles",
GOF.Preferences.get_default (), "show-hidden-files", GLib.SettingsBindFlags.DEFAULT);
Preferences.settings.bind ("show-remote-thumbnails",
GOF.Preferences.get_default (), "show-remote-thumbnails", GLib.SettingsBindFlags.DEFAULT);
Preferences.settings.bind ("confirm-trash",
GOF.Preferences.get_default (), "confirm-trash", GLib.SettingsBindFlags.DEFAULT);
Preferences.settings.bind ("date-format",
GOF.Preferences.get_default (), "date-format", GLib.SettingsBindFlags.DEFAULT);
Preferences.settings.bind ("interpret-desktop-files",
GOF.Preferences.get_default (), "interpret-desktop-files", GLib.SettingsBindFlags.DEFAULT);
Preferences.settings.bind ("force-icon-size",
GOF.Preferences.get_default (), "force-icon-size", GLib.SettingsBindFlags.DEFAULT);
}
private void open_windows (File[]? files) {
if (files == null)
open_tabs (null); /* open_tabs () will restore saved tabs or default tab depending on preference */
else {
/* Open windows with tab at each requested location. */
foreach (var file in files) {
create_window (file);
}
}
}
/* All window creation should be done via this function */
public Marlin.View.Window? create_window (File? location,
Marlin.ViewMode viewmode = Marlin.ViewMode.PREFERRED,
int x = -1, int y = -1) {
if (this.get_windows ().length () >= MAX_WINDOWS) {
return null;
}
Marlin.View.Window win;
Gdk.Rectangle? new_win_rect = null;
Gdk.Screen screen = Gdk.Screen.get_default ();
var aw = this.get_active_window ();
if (aw != null) {
/* This is not the first window - determine size and position of new window */
int w, h;
aw.get_size (out w, out h);
/* Calculate difference between the visible width of the window and the width returned by Gtk+,
* which might include client side decorations (shadow) in some versions (bug 756618).
* Assumes top_menu stretches full visible width. */
var tm_aw = ((Marlin.View.Window)aw).top_menu.get_allocated_width ();
int shadow_width = (w - tm_aw) / 2;
shadow_width -= 10; //Allow a small gap between adjacent windows
screen = aw.get_screen ();
if (x <= 0 || y <= 0) {
/* Place holder for auto-tiling code. If missing then new window will be placed
* at the default position (centre of screen) */
} else { /* New window is a dropped tab */
/* Move new window so that centre of upper edge just inside the window is at mouse
* cursor position. This makes it easier for used to readjust window position with mouse if required.
*/
x -= (shadow_width + w / 2);
y -= (shadow_width + 6);
new_win_rect = {x, y, w, h};
}
}
/* New window will not size or show itself if new_win_rect is not null */
win = new Marlin.View.Window (this, screen, new_win_rect == null);
this.add_window (win as Gtk.Window);
plugins.interface_loaded (win as Gtk.Widget);
if (location != null) {
win.add_tab (location, viewmode);
}
if (new_win_rect != null) {
move_resize_window (win, new_win_rect);
win.show ();
}
return win;
}
private void open_tabs (File[]? files, Gdk.Screen screen = Gdk.Screen.get_default ()) {
Marlin.View.Window window = null;
/* Get the first window, if any, else create a new window */
if (windows_exist ()) {
window = (this.get_windows ()).data as Marlin.View.Window;
} else {
window = create_window (null); /* Do not add a tab on creation */
if (window == null) { /* Maximum number of windows reached */
return;
}
}
if (files == null) {
/* Restore session if settings allow */
if (!Preferences.settings.get_boolean ("restore-tabs") || window.restore_tabs () < 1) {
/* Open a tab pointing at the default location if no tabs restored*/
var location = File.new_for_path (Environment.get_home_dir ());
window.add_tab (location, Marlin.ViewMode.PREFERRED);
}
} else {
/* Open tabs at each requested location */
foreach (var file in files)
window.add_tab (file, Marlin.ViewMode.PREFERRED);
}
}
private bool windows_exist () {
unowned List<weak Gtk.Window> windows = this.get_windows ();
return (windows != null && windows.data != null);
}
private void move_resize_window (Gtk.Window win, Gdk.Rectangle? rect) {
if (rect == null) {
return;
}
if (rect.x > 0 && rect.y > 0) {
win.move (rect.x, rect.y);
}
if (rect.width > 0 && rect.height > 0) {
win.resize (rect.width, rect.height);
}
win.show ();
}
}
|