Skip to content

Commit 1937089

Browse files
committed
templates: Improve invalid XDG_TEMPLATES_DIR handling.
When xdg-user-dirs-update is executed, entries with missing locations are set to $HOME. The nemo_should_use_templates_directory() already guards against trying to load templates from that location, but our new prefs Template page wasn't allowing for this (and would attempt to recurse every file in the user's home directory to add to its template list). Create the Templates folder and update XDG dirs *only* if the user attempts to add templates or open the templates folder directly, otherwise continue ignoring the missing or mis- configured directory. Fixes #3652.
1 parent 5e7fe5b commit 1937089

File tree

5 files changed

+84
-44
lines changed

5 files changed

+84
-44
lines changed

libnemo-private/nemo-file-utilities.c

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
static void update_xdg_dir_cache (void);
5252
static void schedule_user_dirs_changed (void);
5353
static void desktop_dir_changed (void);
54+
static void update_xdg_user_dir (const char *type, const char *path);
5455
static GFile *nemo_find_file_insensitive_next (GFile *parent, const gchar *name);
5556

5657
char *
@@ -305,6 +306,32 @@ parse_xdg_dirs (const char *config_file)
305306
static XdgDirEntry *cached_xdg_dirs = NULL;
306307
static GFileMonitor *cached_xdg_dirs_monitor = NULL;
307308

309+
static void
310+
update_xdg_user_dir (const char *type, const char *path)
311+
{
312+
char *argv[5];
313+
int i;
314+
315+
i = 0;
316+
argv[i++] = (char *)"xdg-user-dirs-update";
317+
argv[i++] = (char *)"--set";
318+
argv[i++] = (char *)type;
319+
argv[i++] = (char *)path;
320+
argv[i++] = NULL;
321+
322+
/* We do this sync, to avoid possible race-conditions
323+
if multiple dirs change at the same time. Its
324+
blocking the main thread, but these updates should
325+
be very rare and very fast. */
326+
g_spawn_sync (NULL,
327+
argv, NULL,
328+
G_SPAWN_SEARCH_PATH |
329+
G_SPAWN_STDOUT_TO_DEV_NULL |
330+
G_SPAWN_STDERR_TO_DEV_NULL,
331+
NULL, NULL,
332+
NULL, NULL, NULL, NULL);
333+
}
334+
308335
static void
309336
xdg_dir_changed (NemoFile *file,
310337
XdgDirEntry *dir)
@@ -318,30 +345,10 @@ xdg_dir_changed (NemoFile *file,
318345
path = g_file_get_path (location);
319346

320347
if (path) {
321-
char *argv[5];
322-
int i;
323-
324348
g_free (dir->path);
325349
dir->path = path;
326350

327-
i = 0;
328-
argv[i++] = (char *)"xdg-user-dirs-update";
329-
argv[i++] = (char *)"--set";
330-
argv[i++] = dir->type;
331-
argv[i++] = dir->path;
332-
argv[i++] = NULL;
333-
334-
/* We do this sync, to avoid possible race-conditions
335-
if multiple dirs change at the same time. Its
336-
blocking the main thread, but these updates should
337-
be very rare and very fast. */
338-
g_spawn_sync (NULL,
339-
argv, NULL,
340-
G_SPAWN_SEARCH_PATH |
341-
G_SPAWN_STDOUT_TO_DEV_NULL |
342-
G_SPAWN_STDERR_TO_DEV_NULL,
343-
NULL, NULL,
344-
NULL, NULL, NULL, NULL);
351+
update_xdg_user_dir (dir->type, dir->path);
345352
g_reload_user_special_dirs_cache ();
346353
schedule_user_dirs_changed ();
347354
desktop_dir_changed ();
@@ -608,15 +615,35 @@ nemo_get_templates_directory (void)
608615
}
609616

610617
void
611-
nemo_create_templates_directory (void)
618+
nemo_ensure_valid_templates_directory (void)
612619
{
613-
char *dir;
620+
/* This is called only at points where the user would expect the Templates
621+
* directory to exist:
622+
* - Template prefs, adding a template via DND or the "New" button.
623+
* - Template prefs - the Folder icon.
624+
* - MenuBar->Go->Templates - navigating directly to the Templates dir.
625+
*
626+
* Otherwise, we're fine without it, if the user never intends to use templates.
627+
*/
628+
g_autofree gchar *templates_dir = NULL;
629+
const char *home_dir;
614630

615-
dir = nemo_get_templates_directory ();
616-
if (!g_file_test (dir, G_FILE_TEST_EXISTS)) {
617-
g_mkdir (dir, DEFAULT_NEMO_DIRECTORY_MODE);
618-
}
619-
g_free (dir);
631+
templates_dir = nemo_get_xdg_dir ("TEMPLATES");
632+
home_dir = g_get_home_dir ();
633+
634+
if (g_strcmp0 (templates_dir, home_dir) == 0) {
635+
// XDG_TEMPLATES_DIR is $HOME/, fix it.
636+
g_autofree gchar *fixed_templates_dir = NULL;
637+
fixed_templates_dir = g_build_filename (home_dir, "Templates", NULL);
638+
g_mkdir (fixed_templates_dir, DEFAULT_NEMO_DIRECTORY_MODE);
639+
640+
update_xdg_user_dir ("TEMPLATES", fixed_templates_dir);
641+
g_reload_user_special_dirs_cache ();
642+
update_xdg_dir_cache ();
643+
} else {
644+
// XDG_TEMPLATES_DIR is reasonable, let's make sure it exists.
645+
g_mkdir (templates_dir, DEFAULT_NEMO_DIRECTORY_MODE);
646+
}
620647
}
621648

622649
char *

libnemo-private/nemo-file-utilities.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ char * nemo_get_gmc_desktop_directory (void);
5757

5858
gboolean nemo_should_use_templates_directory (void);
5959
char * nemo_get_templates_directory (void);
60+
void nemo_ensure_valid_templates_directory (void);
6061
char * nemo_get_templates_directory_uri (void);
61-
void nemo_create_templates_directory (void);
6262

6363
char * nemo_get_searches_directory (void);
6464

src/nemo-interesting-folder-bar.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,14 +236,16 @@ nemo_interesting_folder_bar_new_for_location (NemoView *view, GFile *location)
236236
}
237237

238238
if (type == TYPE_NONE_FOLDER) {
239-
path = nemo_get_templates_directory ();
240-
tmp_loc = g_file_new_for_path (path);
241-
g_free (path);
242-
243-
if (g_file_equal (location, tmp_loc)) {
244-
type = TYPE_TEMPLATES_FOLDER;
239+
if (nemo_should_use_templates_directory ()) {
240+
path = nemo_get_templates_directory ();
241+
tmp_loc = g_file_new_for_path (path);
242+
g_free (path);
243+
244+
if (g_file_equal (location, tmp_loc)) {
245+
type = TYPE_TEMPLATES_FOLDER;
246+
}
247+
g_object_unref (tmp_loc);
245248
}
246-
g_object_unref (tmp_loc);
247249
}
248250

249251
return type == TYPE_NONE_FOLDER ? NULL : nemo_interesting_folder_bar_new (view, type);

src/nemo-template-config-widget.c

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,11 @@ refresh_widget (NemoTemplateConfigWidget *widget)
221221

222222
gchar *path = NULL;
223223

224-
path = nemo_get_templates_directory ();
225-
populate_from_directory (widget, path);
226-
g_clear_pointer (&path, g_free);
224+
if (nemo_should_use_templates_directory ()) {
225+
path = nemo_get_templates_directory ();
226+
populate_from_directory (widget, path);
227+
g_clear_pointer (&path, g_free);
228+
}
227229

228230
if (widget->templates == NULL) {
229231
GtkWidget *empty_label = gtk_label_new (NULL);
@@ -362,7 +364,8 @@ start_renaming_selected_row (NemoTemplateConfigWidget *widget, GtkWidget *row)
362364
}
363365

364366
static void
365-
import_template (const gchar *filename)
367+
import_template (NemoTemplateConfigWidget *widget,
368+
const gchar *filename)
366369
{
367370
GFile *source, *dest;
368371
gchar *basename, *template_dir, *dest_path;
@@ -385,6 +388,7 @@ import_template (const gchar *filename)
385388
}
386389

387390
basename = g_file_get_basename (source);
391+
nemo_ensure_valid_templates_directory ();
388392
template_dir = nemo_get_templates_directory ();
389393
dest_path = g_build_filename (template_dir, basename, NULL);
390394
dest = g_file_new_for_path (dest_path);
@@ -396,6 +400,10 @@ import_template (const gchar *filename)
396400
g_free (dest_path);
397401
g_object_unref (source);
398402
g_object_unref (dest);
403+
404+
if (g_list_length (widget->dir_monitors) == 0) {
405+
refresh_widget (widget);
406+
}
399407
}
400408

401409
static gboolean
@@ -436,7 +444,7 @@ on_new_template_clicked (GtkWidget *button, gpointer user_data)
436444
gchar *selected_filename = gtk_file_chooser_get_filename (file_chooser);
437445

438446
if (selected_filename != NULL) {
439-
import_template (selected_filename);
447+
import_template (widget, selected_filename);
440448
}
441449

442450
g_free (selected_filename);
@@ -485,6 +493,7 @@ static void
485493
on_open_folder_clicked (GtkWidget *button, NemoTemplateConfigWidget *widget)
486494
{
487495
gchar *path = NULL;
496+
nemo_ensure_valid_templates_directory ();
488497
path = nemo_get_templates_directory ();
489498
GFile *location = g_file_new_for_path (path);
490499

@@ -507,6 +516,7 @@ on_drag_data_received(GtkWidget *widget,
507516
guint time,
508517
gpointer user_data)
509518
{
519+
NemoTemplateConfigWidget *self = NEMO_TEMPLATE_CONFIG_WIDGET (user_data);
510520
gboolean success = FALSE;
511521

512522
if (data != NULL && gtk_selection_data_get_length(data) >= 0) {
@@ -517,14 +527,14 @@ on_drag_data_received(GtkWidget *widget,
517527

518528
if (uris != NULL && uris[0] != NULL) {
519529
for (int i = 0; uris[i] != NULL; i++) {
520-
import_template (uris[i]);
530+
import_template (self, uris[i]);
521531
}
522532

523533
g_strfreev(uris);
524534
success = TRUE;
525535
}
526536
} else if (target_type == TARGET_TEXT_PLAIN) {
527-
import_template ((const gchar *) raw_data);
537+
import_template (self, (const gchar *) raw_data);
528538
success = TRUE;
529539
}
530540
}
@@ -655,7 +665,7 @@ nemo_template_config_widget_init (NemoTemplateConfigWidget *self)
655665
GDK_ACTION_COPY);
656666

657667
g_signal_connect(NEMO_CONFIG_BASE_WIDGET (self)->listbox, "drag-data-received",
658-
G_CALLBACK(on_drag_data_received), label);
668+
G_CALLBACK(on_drag_data_received), self);
659669
g_signal_connect(NEMO_CONFIG_BASE_WIDGET (self)->listbox, "drag-motion",
660670
G_CALLBACK(on_drag_motion), NULL);
661671

src/nemo-window-menus.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ action_go_to_templates_callback (GtkAction *action,
200200
window = NEMO_WINDOW (user_data);
201201
slot = nemo_window_get_active_slot (window);
202202

203+
nemo_ensure_valid_templates_directory ();
203204
path = nemo_get_templates_directory ();
204205
location = g_file_new_for_path (path);
205206
g_free (path);

0 commit comments

Comments
 (0)