Fixed a memory leak in src/pcmanfm.c.
[lxde/pcmanfm.git] / src / pcmanfm.c
CommitLineData
b6e3c554
HJYP
1/*
2 * pcmanfm.c
d3451adb 3 *
7b7d923f 4 * Copyright 2009 - 2010 Hong Jen Yee (PCMan) <pcman.tw@gmail.com>
d3451adb 5 *
b6e3c554
HJYP
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
d3451adb 10 *
b6e3c554
HJYP
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
d3451adb 15 *
b6e3c554
HJYP
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
08e70fea 22#ifdef HAVE_CONFIG_H
b6e3c554 23#include <config.h>
08e70fea
HJYP
24#endif
25
b6e3c554 26#include <gtk/gtk.h>
1098cfe7 27#include <gdk/gdkx.h>
b6e3c554 28#include <stdio.h>
f8f2bfad
HJYP
29#include <glib/gi18n.h>
30
31#include <stdlib.h>
32#include <string.h>
33/* socket is used to keep single instance */
34#include <sys/types.h>
f8f2bfad
HJYP
35#include <signal.h>
36#include <unistd.h> /* for getcwd */
b6e3c554 37
20c0bc9a 38#include <libfm/fm-gtk.h>
4d55886c 39#include "app-config.h"
b6e3c554 40#include "main-win.h"
8505a8ef 41#include "desktop.h"
71f82759 42#include "volume-manager.h"
c5fccf1d 43#include "pref.h"
df6826e0 44#include "pcmanfm.h"
cc448d7f 45#include "single-inst.h"
f8f2bfad 46
94435bc3 47static int signal_pipe[2] = {-1, -1};
f8f2bfad 48gboolean daemon_mode = FALSE;
3d141640 49static guint save_config_idle = 0;
f8f2bfad 50
f8f2bfad 51static char** files_to_open = NULL;
cc448d7f 52static int n_files_to_open = 0;
f970e846 53static char* profile = NULL;
f8f2bfad
HJYP
54static gboolean no_desktop = FALSE;
55static gboolean show_desktop = FALSE;
56static gboolean desktop_off = FALSE;
57static gboolean desktop_running = FALSE;
107cdb22 58/* static gboolean new_tab = FALSE; */
f8f2bfad
HJYP
59static int show_pref = 0;
60static gboolean desktop_pref = FALSE;
61static char* set_wallpaper = NULL;
107cdb22 62static char* wallpaper_mode = NULL;
9197e75e 63/* static gboolean new_win = FALSE; */
f8f2bfad 64static gboolean find_files = FALSE;
5b890032 65static char* ipc_cwd = NULL;
f8f2bfad
HJYP
66
67static int n_pcmanfm_ref = 0;
68
69static GOptionEntry opt_entries[] =
8505a8ef 70{
cda6259f 71 /* options only acceptable by first pcmanfm instance. These options are not passed through IPC */
a48f9fc8 72 { "profile", 'p', 0, G_OPTION_ARG_STRING, &profile, N_("Name of configuration profile"), "<profile name>" },
cda6259f
HJYP
73 { "daemon-mode", 'd', 0, G_OPTION_ARG_NONE, &daemon_mode, N_("Run PCManFM as a daemon"), NULL },
74 { "no-desktop", '\0', 0, G_OPTION_ARG_NONE, &no_desktop, N_("No function. Just to be compatible with nautilus"), NULL },
75
76 /* options that are acceptable for every instance of pcmanfm and will be passed through IPC. */
f8f2bfad
HJYP
77 { "desktop", '\0', 0, G_OPTION_ARG_NONE, &show_desktop, N_("Launch desktop manager"), NULL },
78 { "desktop-off", '\0', 0, G_OPTION_ARG_NONE, &desktop_off, N_("Turn off desktop manager if it's running"), NULL },
f8f2bfad 79 { "desktop-pref", '\0', 0, G_OPTION_ARG_NONE, &desktop_pref, N_("Open desktop preference dialog"), NULL },
f970e846 80 { "set-wallpaper", 'w', 0, G_OPTION_ARG_FILENAME, &set_wallpaper, N_("Set desktop wallpaper"), N_("<image file>") },
107cdb22 81 { "wallpaper-mode", '\0', 0, G_OPTION_ARG_STRING, &wallpaper_mode, N_("Set mode of desktop wallpaper. <mode>=(color|stretch|fit|center|tile)"), N_("<mode>") },
a48f9fc8 82 { "show-pref", '\0', 0, G_OPTION_ARG_INT, &show_pref, N_("Open preference dialog. 'n' is number of the page you want to show (1, 2, 3...)."), "n" },
9197e75e 83 /* { "new-win", '\0', 'n', G_OPTION_ARG_NONE, &new_win, N_("Open new window"), NULL }, */
1d071341 84 /* { "find-files", 'f', 0, G_OPTION_ARG_NONE, &find_files, N_("Open Find Files utility"), NULL }, */
f8f2bfad
HJYP
85 {G_OPTION_REMAINING, 0, 0, G_OPTION_ARG_FILENAME_ARRAY, &files_to_open, NULL, N_("[FILE1, FILE2,...]")},
86 { NULL }
87};
88
107cdb22
HJYP
89static const char* valid_wallpaper_modes[] = {"color", "stretch", "fit", "center", "tile"};
90
f8f2bfad 91static gboolean pcmanfm_run();
b6e3c554 92
94435bc3
HJYP
93/* it's not safe to call gtk+ functions in unix signal handler
94 * since the process is interrupted here and the state of gtk+ is unpredictable. */
95static void unix_signal_handler(int sig_num)
96{
97 /* postpond the signal handling by using a pipe */
3aebc679
AG
98 if (write(signal_pipe[1], &sig_num, sizeof(sig_num)) != sizeof(sig_num)) {
99 g_critical("cannot bounce the signal, stop");
100 exit(2);
101 }
94435bc3
HJYP
102}
103
104static gboolean on_unix_signal(GIOChannel* ch, GIOCondition cond, gpointer user_data)
105{
106 int sig_num;
3aebc679 107 g_io_channel_read_chars(ch, (gchar*)&sig_num, 1, NULL, NULL);
94435bc3
HJYP
108 switch(sig_num)
109 {
110 case SIGTERM:
111 default:
112 gtk_main_quit();
113 }
114 return TRUE;
115}
116
1098cfe7 117static void single_inst_cb(const char* cwd, int screen_num)
cc448d7f 118{
cda6259f
HJYP
119 g_free(ipc_cwd);
120 ipc_cwd = g_strdup(cwd);
121
122 if(files_to_open)
cc448d7f 123 {
cda6259f 124 int i;
6033d802 125 n_files_to_open = g_strv_length(files_to_open);
cda6259f
HJYP
126 /* canonicalize filename if needed. */
127 for(i = 0; i < n_files_to_open; ++i)
cc448d7f 128 {
cda6259f
HJYP
129 char* file = files_to_open[i];
130 char* scheme = g_uri_parse_scheme(file);
6033d802 131 g_debug("file: %s", file);
cda6259f 132 if(scheme) /* a valid URI */
cc448d7f 133 {
cda6259f
HJYP
134 /* FIXME: should we canonicalize URIs? and how about file:///? */
135 g_free(scheme);
136 }
137 else /* a file path */
138 {
139 files_to_open[i] = fm_canonicalize_filename(file, cwd);
140 g_free(file);
cc448d7f 141 }
cc448d7f 142 }
cc448d7f 143 }
cda6259f 144 pcmanfm_run();
cc448d7f
HJYP
145}
146
b6e3c554
HJYP
147int main(int argc, char** argv)
148{
4d55886c 149 FmConfig* config;
f8f2bfad 150 GError* err = NULL;
cda6259f 151
f8f2bfad
HJYP
152#ifdef ENABLE_NLS
153 bindtextdomain ( GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR );
154 bind_textdomain_codeset ( GETTEXT_PACKAGE, "UTF-8" );
155 textdomain ( GETTEXT_PACKAGE );
156#endif
157
158 /* initialize GTK+ and parse the command line arguments */
159 if(G_UNLIKELY(!gtk_init_with_args(&argc, &argv, "", opt_entries, GETTEXT_PACKAGE, &err)))
160 {
161 g_printf("%s\n", err->message);
c5fccf1d 162 g_error_free(err);
f8f2bfad
HJYP
163 return 1;
164 }
165
cc448d7f 166 /* ensure that there is only one instance of pcmanfm. */
1098cfe7 167 switch(single_inst_init("pcmanfm", single_inst_cb, opt_entries + 3, gdk_x11_get_default_screen()))
cc448d7f
HJYP
168 {
169 case SINGLE_INST_CLIENT: /* we're not the first instance. */
cda6259f 170 single_inst_finalize();
cc448d7f
HJYP
171 gdk_notify_startup_complete();
172 return 0;
173 case SINGLE_INST_ERROR: /* error happened. */
174 single_inst_finalize();
175 return 1;
3aebc679 176 case SINGLE_INST_SERVER: ; /* FIXME */
cc448d7f 177 }
f8f2bfad 178
94435bc3
HJYP
179 if(pipe(signal_pipe) == 0)
180 {
181 GIOChannel* ch = g_io_channel_unix_new(signal_pipe[0]);
182 g_io_add_watch(ch, G_IO_IN|G_IO_PRI, (GIOFunc)on_unix_signal, NULL);
183 g_io_channel_unref(ch);
184
185 /* intercept signals */
ae2947d9
HJYP
186 // signal( SIGPIPE, SIG_IGN );
187 signal( SIGHUP, unix_signal_handler );
94435bc3 188 signal( SIGTERM, unix_signal_handler );
e2f270e5 189#ifdef SIGPOLL /* bug #3528311 */
ae2947d9 190 signal( SIGPOLL, unix_signal_handler );
e2f270e5 191#endif
ae2947d9 192 signal( SIGHUP, unix_signal_handler );
94435bc3 193 }
b6e3c554 194
f970e846 195 config = fm_app_config_new(); /* this automatically load libfm config file. */
17d12cc9 196
f970e846 197 /* load pcmanfm-specific config file */
17d12cc9 198 fm_app_config_load_from_profile(FM_APP_CONFIG(config), profile);
f970e846 199
94435bc3 200 fm_gtk_init(config);
f8f2bfad
HJYP
201 /* the main part */
202 if(pcmanfm_run())
203 {
71f82759 204 fm_volume_manager_init();
94435bc3 205 gtk_main();
f8f2bfad
HJYP
206 if(desktop_running)
207 fm_desktop_manager_finalize();
3d141640
HJYP
208
209 pcmanfm_save_config(TRUE);
210 if(save_config_idle)
211 {
212 g_source_remove(save_config_idle);
213 save_config_idle = 0;
214 }
71f82759 215 fm_volume_manager_finalize();
f8f2bfad 216 }
cc448d7f
HJYP
217
218 single_inst_finalize();
f8f2bfad 219 fm_gtk_finalize();
ae2947d9 220
f8f2bfad 221 g_object_unref(config);
94435bc3 222 return 0;
f8f2bfad
HJYP
223}
224
ee12f37e
HJYP
225static FmJobErrorAction on_file_info_job_error(FmFileInfoJob* job, GError* err, FmJobErrorSeverity severity, gpointer user_data)
226{
16a17d6b 227 if(err->domain == G_IO_ERROR)
ee12f37e 228 {
16a17d6b
HJYP
229 if(err->code == G_IO_ERROR_NOT_MOUNTED)
230 {
231 if(fm_mount_path(NULL, fm_file_info_job_get_current(job), TRUE))
232 return FM_JOB_RETRY;
233 }
234 else if(err->code == G_IO_ERROR_FAILED_HANDLED)
235 return FM_JOB_CONTINUE;
ee12f37e 236 }
5089e13a 237 fm_show_error(NULL, NULL, err->message);
ee12f37e
HJYP
238 return FM_JOB_CONTINUE;
239}
f8f2bfad
HJYP
240
241gboolean pcmanfm_run()
242{
243 gboolean ret = TRUE;
f8f2bfad 244
f8f2bfad
HJYP
245 if(!files_to_open)
246 {
247 /* Launch desktop manager */
248 if(show_desktop)
249 {
250 if(!desktop_running)
251 {
252 fm_desktop_manager_init();
253 desktop_running = TRUE;
254 }
19abb8bf 255 show_desktop = FALSE;
f8f2bfad
HJYP
256 return TRUE;
257 }
258 else if(desktop_off)
259 {
260 if(desktop_running)
261 {
262 desktop_running = FALSE;
263 fm_desktop_manager_finalize();
264 }
19abb8bf 265 desktop_off = FALSE;
f8f2bfad
HJYP
266 return FALSE;
267 }
268 else if(show_pref > 0)
269 {
c5fccf1d
HJYP
270 fm_edit_preference(NULL, show_pref - 1);
271 show_pref = 0;
f8f2bfad
HJYP
272 return TRUE;
273 }
274 else if(desktop_pref)
275 {
c5fccf1d 276 fm_desktop_preference();
19abb8bf 277 desktop_pref = FALSE;
f8f2bfad
HJYP
278 return TRUE;
279 }
107cdb22 280 else
f8f2bfad 281 {
107cdb22
HJYP
282 gboolean need_to_exit = (wallpaper_mode || set_wallpaper);
283 gboolean wallpaper_changed = FALSE;
284 if(set_wallpaper) /* a new wallpaper is assigned */
285 {
286 /* g_debug("\'%s\'", set_wallpaper); */
287 /* Make sure this is a support image file. */
288 if(gdk_pixbuf_get_file_info(set_wallpaper, NULL, NULL))
289 {
290 if(app_config->wallpaper)
291 g_free(app_config->wallpaper);
292 app_config->wallpaper = set_wallpaper;
293 set_wallpaper = NULL;
294 if(! wallpaper_mode) /* if wallpaper mode is not specified */
295 {
296 /* do not use solid color mode; otherwise wallpaper won't be shown. */
297 if(app_config->wallpaper_mode == FM_WP_COLOR)
298 app_config->wallpaper_mode = FM_WP_FIT;
299 }
300 wallpaper_changed = TRUE;
301 }
302 }
303
304 if(wallpaper_mode)
305 {
6f0f1950 306 guint i = 0;
107cdb22
HJYP
307 for(i = 0; i < G_N_ELEMENTS(valid_wallpaper_modes); ++i)
308 {
309 if(strcmp(valid_wallpaper_modes[i], wallpaper_mode) == 0)
310 {
311 if(i != app_config->wallpaper_mode)
312 {
313 app_config->wallpaper_mode = i;
314 wallpaper_changed = TRUE;
315 }
316 break;
317 }
318 }
319 g_free(wallpaper_mode);
320 wallpaper_mode = NULL;
321 }
322
323 if(wallpaper_changed)
f8f2bfad 324 {
4b3e1fe9 325 fm_config_emit_changed(FM_CONFIG(app_config), "wallpaper");
17d12cc9 326 fm_app_config_save_profile(app_config, profile);
f8f2bfad 327 }
107cdb22
HJYP
328
329 if(need_to_exit)
330 return FALSE;
f8f2bfad
HJYP
331 }
332 }
333
334 if(G_UNLIKELY(find_files))
335 {
336 /* FIXME: find files */
337 }
338 else
339 {
df6826e0
HJYP
340 if(files_to_open)
341 {
342 char** filename;
3aebc679 343 FmFileInfoJob* job = fm_file_info_job_new(NULL, 0);
cacf261a 344 FmPath* cwd = NULL;
d951adb8 345 GList* infos;
df6826e0
HJYP
346 for(filename=files_to_open; *filename; ++filename)
347 {
cacf261a 348 FmPath* path;
9197e75e
HJYP
349 if( **filename == '/') /* absolute path */
350 path = fm_path_new_for_path(*filename);
351 else if(strstr(*filename, ":/") ) /* URI */
352 path = fm_path_new_for_uri(*filename);
0d8dce1a
HJYP
353 else if( strcmp(*filename, "~") == 0 ) /* special case for home dir */
354 {
355 path = fm_path_get_home();
356 fm_main_win_add_win(NULL, path);
357 continue;
358 }
cacf261a
HJYP
359 else /* basename */
360 {
361 if(G_UNLIKELY(!cwd))
362 {
363 /* FIXME: This won't work if those filenames are passed via IPC since the receiving process has different cwd. */
9197e75e 364 /* FIXME: should we use ipc_cwd here? */
cacf261a 365 char* cwd_str = g_get_current_dir();
653af525 366 cwd = fm_path_new_for_str(cwd_str);
cacf261a
HJYP
367 g_free(cwd_str);
368 }
369 path = fm_path_new_relative(cwd, *filename);
370 }
3aebc679 371 fm_file_info_job_add(job, path);
df6826e0
HJYP
372 fm_path_unref(path);
373 }
cacf261a
HJYP
374 if(cwd)
375 fm_path_unref(cwd);
ee12f37e 376 g_signal_connect(job, "error", G_CALLBACK(on_file_info_job_error), NULL);
3aebc679 377 fm_job_run_sync_with_mainloop(FM_JOB(job));
5a89e062 378 infos = fm_file_info_list_peek_head_link(job->file_infos);
d951adb8 379 fm_launch_files_simple(NULL, NULL, infos, pcmanfm_open_folder, NULL);
df6826e0 380 g_object_unref(job);
cacf261a 381 ret = (n_pcmanfm_ref >= 1); /* if there is opened window, return true to run the main loop. */
2b46b521
HJYP
382
383 g_strfreev(files_to_open);
384 files_to_open = NULL;
df6826e0
HJYP
385 }
386 else
f8f2bfad 387 {
bc88a8dc
HJYP
388 static gboolean first_run = TRUE;
389 if(first_run && daemon_mode)
3d141640 390 {
bc88a8dc
HJYP
391 /* If the function is called the first time and we're in daemon mode,
392 * don't open any folder.
393 * Checking if pcmanfm_run() is called the first time is needed to fix
394 * #3397444 - pcmanfm dont show window in daemon mode if i call 'pcmanfm' */
395 }
396 else
397 {
398 /* If we're not in daemon mode, or pcmanfm_run() is called because another
399 * instance send signal to us, open cwd by default. */
3d141640
HJYP
400 FmPath* path;
401 char* cwd = ipc_cwd ? ipc_cwd : g_get_current_dir();
402 path = fm_path_new_for_path(cwd);
403 fm_main_win_add_win(NULL, path);
404 fm_path_unref(path);
405 g_free(cwd);
406 ipc_cwd = NULL;
407 }
bc88a8dc 408 first_run = FALSE;
f8f2bfad
HJYP
409 }
410 }
411 return ret;
412}
413
414/* After opening any window/dialog/tool, this should be called. */
415void pcmanfm_ref()
416{
417 ++n_pcmanfm_ref;
418 /* g_debug("ref: %d", n_pcmanfm_ref); */
419}
420
421/* After closing any window/dialog/tool, this should be called.
422 * If the last window is closed and we are not a deamon, pcmanfm will quit.
423 */
424void pcmanfm_unref()
425{
426 --n_pcmanfm_ref;
427 /* g_debug("unref: %d, daemon_mode=%d, desktop_running=%d", n_pcmanfm_ref, daemon_mode, desktop_running); */
428 if( 0 == n_pcmanfm_ref && !daemon_mode && !desktop_running )
429 gtk_main_quit();
b6e3c554 430}
df6826e0
HJYP
431
432gboolean pcmanfm_open_folder(GAppLaunchContext* ctx, GList* folder_infos, gpointer user_data, GError** err)
433{
df6826e0
HJYP
434 GList* l = folder_infos;
435 for(; l; l=l->next)
436 {
437 FmFileInfo* fi = (FmFileInfo*)l->data;
0b951729 438 fm_main_win_open_in_last_active(fm_file_info_get_path(fi));
df6826e0
HJYP
439 }
440 return TRUE;
441}
dcecce78 442
3d141640 443static gboolean on_save_config_idle(gpointer user_data)
dcecce78 444{
3d141640
HJYP
445 pcmanfm_save_config(TRUE);
446 save_config_idle = 0;
447 return FALSE;
448}
449
450void pcmanfm_save_config(gboolean immediate)
451{
452 if(immediate)
453 {
454 fm_config_save(fm_config, NULL);
455 fm_app_config_save_profile(app_config, profile);
456 }
457 else
458 {
459 /* install an idle handler to save the config file. */
460 if( 0 == save_config_idle)
461 save_config_idle = g_idle_add_full(G_PRIORITY_LOW, (GSourceFunc)on_save_config_idle, NULL, NULL);
462 }
dcecce78 463}
c56a211a
HJYP
464
465void pcmanfm_open_folder_in_terminal(GtkWindow* parent, FmPath* dir)
466{
467 GAppInfo* app;
c56a211a
HJYP
468 char** argv;
469 int argc;
470 if(!fm_config->terminal)
471 {
5089e13a 472 fm_show_error(parent, NULL, _("Terminal emulator is not set."));
c56a211a
HJYP
473 fm_edit_preference(parent, PREF_ADVANCED);
474 return;
475 }
476 if(!g_shell_parse_argv(fm_config->terminal, &argc, &argv, NULL))
477 return;
478 app = g_app_info_create_from_commandline(argv[0], NULL, 0, NULL);
479 g_strfreev(argv);
480 if(app)
481 {
482 GError* err = NULL;
3aebc679 483 GdkAppLaunchContext* ctx = gdk_app_launch_context_new();
c56a211a 484 char* cwd_str;
1dfc8e4c 485 char* old_cwd = g_get_current_dir();
c56a211a
HJYP
486
487 if(fm_path_is_native(dir))
488 cwd_str = fm_path_to_str(dir);
489 else
490 {
491 GFile* gf = fm_path_to_gfile(dir);
492 cwd_str = g_file_get_path(gf);
493 g_object_unref(gf);
494 }
3aebc679
AG
495 gdk_app_launch_context_set_screen(ctx, parent ? gtk_widget_get_screen(GTK_WIDGET(parent)) : gdk_screen_get_default());
496 gdk_app_launch_context_set_timestamp(ctx, gtk_get_current_event_time());
c56a211a
HJYP
497 g_chdir(cwd_str); /* FIXME: currently we don't have better way for this. maybe a wrapper script? */
498 g_free(cwd_str);
1dfc8e4c 499
3aebc679 500 if(!g_app_info_launch(app, NULL, G_APP_LAUNCH_CONTEXT(ctx), &err))
c56a211a 501 {
5089e13a 502 fm_show_error(parent, NULL, err->message);
c56a211a
HJYP
503 g_error_free(err);
504 }
505 g_object_unref(ctx);
506 g_object_unref(app);
1dfc8e4c
HJYP
507
508 /* switch back to old cwd and fix #3114626 - PCManFM 0.9.9 Umount partitions problem */
509 g_chdir(old_cwd); /* This is really dirty, but we don't have better solution now. */
510 g_free(old_cwd);
c56a211a
HJYP
511 }
512}
7cb06c27
HJYP
513
514/* FIXME: Need to load content of ~/Templates and list available templates in popup menus. */
515void pcmanfm_create_new(GtkWindow* parent, FmPath* cwd, const char* templ)
516{
517 GError* err = NULL;
518 FmPath* dest;
519 char* basename;
cac3de08 520 const char* msg;
3aebc679 521 //FmMainWin* win = FM_MAIN_WIN(parent);
7cb06c27 522_retry:
cac3de08
HJYP
523 if(templ == TEMPL_NAME_FOLDER)
524 msg = N_("Enter a name for the newly created folder:");
525 else
526 msg = N_("Enter a name for the newly created file:");
527 basename = fm_get_user_input(parent, _("Create New..."), _(msg), _("New"));
7cb06c27
HJYP
528 if(!basename)
529 return;
530
531 dest = fm_path_new_child(cwd, basename);
532 g_free(basename);
533
534 if( templ == TEMPL_NAME_FOLDER )
535 {
536 GFile* gf = fm_path_to_gfile(dest);
537 if(!g_file_make_directory(gf, NULL, &err))
538 {
98daf506 539 if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_EXISTS)
7cb06c27
HJYP
540 {
541 fm_path_unref(dest);
542 g_error_free(err);
543 g_object_unref(gf);
544 err = NULL;
545 goto _retry;
546 }
5089e13a 547 fm_show_error(parent, NULL, err->message);
7cb06c27
HJYP
548 g_error_free(err);
549 }
550
551 if(!err) /* select the newly created file */
552 {
553 /*FIXME: this doesn't work since the newly created file will
554 * only be shown after file-created event was fired on its
555 * folder's monitor and after FmFolder handles it in idle
556 * handler. So, we cannot select it since it's not yet in
557 * the folder model now. */
558 /* fm_folder_view_select_file_path(fv, dest); */
559 }
560 g_object_unref(gf);
561 }
562 else if( templ == TEMPL_NAME_BLANK )
563 {
564 GFile* gf = fm_path_to_gfile(dest);
565 GFileOutputStream* f = g_file_create(gf, G_FILE_CREATE_NONE, NULL, &err);
566 if(f)
567 {
568 g_output_stream_close(G_OUTPUT_STREAM(f), NULL, NULL);
569 g_object_unref(f);
570 }
571 else
572 {
98daf506 573 if(err->domain == G_IO_ERROR && err->code == G_IO_ERROR_EXISTS)
7cb06c27
HJYP
574 {
575 fm_path_unref(dest);
576 g_error_free(err);
577 g_object_unref(gf);
578 err = NULL;
579 goto _retry;
580 }
5089e13a 581 fm_show_error(parent, NULL, err->message);
7cb06c27
HJYP
582 g_error_free(err);
583 }
584
585 if(!err) /* select the newly created file */
586 {
587 /*FIXME: this doesn't work since the newly created file will
588 * only be shown after file-created event was fired on its
589 * folder's monitor and after FmFolder handles it in idle
590 * handler. So, we cannot select it since it's not yet in
591 * the folder model now. */
592 /* fm_folder_view_select_file_path(fv, dest); */
593 }
594 g_object_unref(gf);
595 }
596 else /* templates in ~/Templates */
597 {
b2fd837c 598 /* FIXME: need an extended processing with desktop entries support */
653af525 599 FmPath* dir = fm_path_new_for_str(g_get_user_special_dir(G_USER_DIRECTORY_TEMPLATES));
7cb06c27 600 FmPath* template = fm_path_new_child(dir, templ);
9afd9e19 601 fm_copy_file(parent, template, cwd);
7cb06c27 602 fm_path_unref(template);
b2fd837c 603 fm_path_unref(dir);
7cb06c27
HJYP
604 }
605 fm_path_unref(dest);
606}
92f8ca49
HJYP
607
608char* pcmanfm_get_profile_dir(gboolean create)
609{
610 char* dir = g_build_filename(g_get_user_config_dir(), "pcmanfm", profile ? profile : "default", NULL);
611 if(create)
612 g_mkdir_with_parents(dir, 0700);
613 return dir;
614}