static void
get_themes_from_themes_dir (GFile                  *themes_dir,
                            HDAvailableBackgrounds *backgrounds)
{
  GFileEnumerator *enumerator;
  GFileInfo *info;
  GError *error = NULL;

  enumerator = g_file_enumerate_children (themes_dir,
                                          G_FILE_ATTRIBUTE_STANDARD_NAME,
                                          G_FILE_QUERY_INFO_NONE,
                                          NULL,
                                          &error);

  if (error)
    {
      char *path = g_file_get_path (themes_dir);
      g_warning ("%s. Could not enumerate themes_dir %s. %s",
                 __FUNCTION__,
                 path,
                 error->message);
      g_free (path);
      g_error_free (error);
      return;
    }

  do
    {
      info = g_file_enumerator_next_file (enumerator,
                                          NULL,
                                          &error);
      if (error)
        {
          char *path = g_file_get_path (themes_dir);
          g_warning ("%s. Error enumerating themes_dir %s. %s",
                     __FUNCTION__,
                     path,
                     error->message);
          g_free (path);
          g_error_free (error);
          goto cleanup;
        }

      if (info)
        {
          const char *name;

          name = g_file_info_get_name (info);

          if (g_strcmp0 (name, "default") != 0)
            {
              HDBackground *background;
              GFile *theme_dir = g_file_get_child (themes_dir, name);
              GFile *desktop_file = g_file_resolve_relative_path (theme_dir,
                                                                  "./backgrounds/theme_bg.desktop");
              GFile *theme_file = g_file_get_child (theme_dir,
                                                    "index.theme");
              background = hd_theme_background_new (desktop_file,
                                                    theme_file);
              hd_imageset_background_init_async (HD_IMAGESET_BACKGROUND (background),
                                                 NULL,
                                                 (GAsyncReadyCallback) theme_loaded,
                                                 backgrounds);
              g_object_unref (theme_dir);
              g_object_unref (desktop_file);
              g_object_unref (theme_file);
            }

          g_object_unref (info);
        }
    }
  while (info);

cleanup:
  g_object_unref (enumerator);
}
gboolean
e_reap_trash_directory_sync (GFile *trash_directory,
                             gint expiry_in_days,
                             GCancellable *cancellable,
                             GError **error)
{
	GFileEnumerator *file_enumerator;
	GQueue directories = G_QUEUE_INIT;
	GFile *reaping_directory;
	GFileInfo *file_info;
	const gchar *attributes;
	gboolean success = TRUE;
	GError *local_error = NULL;

	g_return_val_if_fail (G_IS_FILE (trash_directory), FALSE);
	g_return_val_if_fail (expiry_in_days > 0, FALSE);

	reaping_directory = g_file_get_child (
		trash_directory, REAPING_DIRECTORY_NAME);

	attributes =
		G_FILE_ATTRIBUTE_STANDARD_NAME ","
		G_FILE_ATTRIBUTE_STANDARD_TYPE ","
		G_FILE_ATTRIBUTE_TIME_MODIFIED;

	file_enumerator = g_file_enumerate_children (
		trash_directory, attributes,
		G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
		cancellable, error);

	if (file_enumerator == NULL)
		return FALSE;

	file_info = g_file_enumerator_next_file (
		file_enumerator, cancellable, &local_error);

	while (file_info != NULL) {
		GFileType file_type;
		GTimeVal mtime;
		GDate *date_now;
		GDate *date_mtime;
		const gchar *name;
		gboolean reap_it;
		gint days_old;

		name = g_file_info_get_name (file_info);
		file_type = g_file_info_get_file_type (file_info);
		g_file_info_get_modification_time (file_info, &mtime);

		/* Calculate how many days ago the file was modified. */
		date_now = g_date_new ();
		g_date_set_time_t (date_now, time (NULL));
		date_mtime = g_date_new ();
		g_date_set_time_val (date_mtime, &mtime);
		days_old = g_date_days_between (date_mtime, date_now);
		g_date_free (date_mtime);
		g_date_free (date_now);

		reap_it =
			(file_type == G_FILE_TYPE_DIRECTORY) &&
			(days_old >= expiry_in_days);

		if (reap_it) {
			GFile *child;

			child = g_file_get_child (trash_directory, name);

			/* If we find an unfinished reaping directory, put
			 * it on the head of the queue so we reap it first. */
			if (g_file_equal (child, reaping_directory))
				g_queue_push_head (&directories, child);
			else
				g_queue_push_tail (&directories, child);
		}

		g_object_unref (file_info);

		file_info = g_file_enumerator_next_file (
			file_enumerator, cancellable, &local_error);
	}

	if (local_error != NULL) {
		g_propagate_error (error, local_error);
		success = FALSE;
	}

	g_object_unref (file_enumerator);

	/* Now delete the directories we've queued up. */
	while (success && !g_queue_is_empty (&directories)) {
		GFile *directory;

		directory = g_queue_pop_head (&directories);

		/* First we rename the directory to prevent it
		 * from being recovered while being deleted. */
		if (!g_file_equal (directory, reaping_directory))
			success = g_file_move (
				directory, reaping_directory,
				G_FILE_COPY_NONE, cancellable,
				NULL, NULL, error);

		if (success)
			success = e_file_recursive_delete_sync (
				reaping_directory, cancellable, error);

		g_object_unref (directory);
	}

	/* Flush the queue in case we aborted on an error. */
	while (!g_queue_is_empty (&directories))
		g_object_unref (g_queue_pop_head (&directories));

	g_object_unref (reaping_directory);

	return success;
}
Exemple #3
0
static gboolean
dlg_add_folder_load_options (DialogData *data,
                             const char *name)
{
    GFile     *options_dir;
    GFile     *options_file;
    char      *file_path;
    GKeyFile  *key_file;
    GError    *error = NULL;
    char      *base_dir = NULL;
    char      *filename = NULL;
    char      *include_files = NULL;
    char      *exclude_files = NULL;
    char      *exclude_folders = NULL;
    gboolean   update;
    gboolean   recursive;
    gboolean   no_symlinks;

    options_dir = get_user_config_subdirectory (ADD_FOLDER_OPTIONS_DIR, TRUE);
    options_file = g_file_get_child (options_dir, name);
    file_path = g_file_get_path (options_file);
    key_file = g_key_file_new ();
    if (! g_key_file_load_from_file (key_file, file_path, G_KEY_FILE_KEEP_COMMENTS, &error)) {
        if (error->code != G_IO_ERROR_NOT_FOUND)
            g_warning ("Could not load options file: %s\n", error->message);
        g_clear_error (&error);
        g_object_unref (options_file);
        g_object_unref (options_dir);
        g_key_file_free (key_file);
        return FALSE;
    }

    base_dir = g_key_file_get_string (key_file, "Options", "base_dir", NULL);
    filename = g_key_file_get_string (key_file, "Options", "filename", NULL);
    include_files = g_key_file_get_string (key_file, "Options", "include_files", NULL);
    exclude_files = g_key_file_get_string (key_file, "Options", "exclude_files", NULL);
    exclude_folders = g_key_file_get_string (key_file, "Options", "exclude_folders", NULL);
    update = g_key_file_get_boolean (key_file, "Options", "update", NULL);
    recursive = g_key_file_get_boolean (key_file, "Options", "recursive", NULL);
    no_symlinks = g_key_file_get_boolean (key_file, "Options", "no_symlinks", NULL);

    sync_widgets_with_options (data,
                               base_dir,
                               filename,
                               include_files,
                               exclude_files,
                               exclude_folders,
                               update,
                               recursive,
                               no_symlinks);

    dlg_add_folder_save_last_used_options (data, file_path);

    g_free (base_dir);
    g_free (filename);
    g_free (include_files);
    g_free (exclude_files);
    g_free (exclude_folders);
    g_key_file_free (key_file);
    g_free (file_path);
    g_object_unref (options_file);
    g_object_unref (options_dir);

    return TRUE;
}
gboolean
athena_link_local_create (const char     *directory_uri,
			    const char     *base_name,
			    const char     *display_name,
			    const char     *image,
			    const char     *target_uri,
			    const GdkPoint *point,
			    int             screen,
			    gboolean        unique_filename)
{
	char *real_directory_uri;
	char *uri, *contents;
	GFile *file;
	GList dummy_list;
	AthenaFileChangesQueuePosition item;

	g_return_val_if_fail (directory_uri != NULL, FALSE);
	g_return_val_if_fail (base_name != NULL, FALSE);
	g_return_val_if_fail (display_name != NULL, FALSE);
	g_return_val_if_fail (target_uri != NULL, FALSE);

	if (eel_uri_is_trash (directory_uri) ||
	    eel_uri_is_search (directory_uri)) {
		return FALSE;
	}

	if (eel_uri_is_desktop (directory_uri)) {
		real_directory_uri = athena_get_desktop_directory_uri ();
	} else {
		real_directory_uri = g_strdup (directory_uri);
	}

	if (unique_filename) {
		uri = athena_ensure_unique_file_name (real_directory_uri,
							base_name, ".desktop");
		if (uri == NULL) {
			g_free (real_directory_uri);
			return FALSE;
		}
		file = g_file_new_for_uri (uri);
		g_free (uri);
	} else {
		char *link_name;
		GFile *dir;

		link_name = g_strdup_printf ("%s.desktop", base_name);

		/* replace '/' with '-', just in case */
		g_strdelimit (link_name, "/", '-');

		dir = g_file_new_for_uri (directory_uri);
		file = g_file_get_child (dir, link_name);

		g_free (link_name);
		g_object_unref (dir);
	}

	g_free (real_directory_uri);

	contents = g_strdup_printf ("[Desktop Entry]\n"
				    "Encoding=UTF-8\n"
				    "Name=%s\n"
				    "Type=Link\n"
				    "URL=%s\n"
				    "%s%s\n",
				    display_name,
				    target_uri,
				    image != NULL ? "Icon=" : "",
				    image != NULL ? image : "");


	if (!g_file_replace_contents (file,
				      contents, strlen (contents),
				      NULL, FALSE,
				      G_FILE_CREATE_NONE,
				      NULL, NULL, NULL)) {
		g_free (contents);
		g_object_unref (file);
		return FALSE;
	}
	g_free (contents);

	dummy_list.data = file;
	dummy_list.next = NULL;
	dummy_list.prev = NULL;
	athena_directory_notify_files_added (&dummy_list);

	if (point != NULL) {
		item.location = file;
		item.set = TRUE;
		item.point.x = point->x;
		item.point.y = point->y;
		item.screen = screen;
		dummy_list.data = &item;
		dummy_list.next = NULL;
		dummy_list.prev = NULL;
	
		athena_directory_schedule_position_set (&dummy_list);
	}

	g_object_unref (file);
	return TRUE;
}
Exemple #5
0
static void
_uri_handle_recurse (GFile *dir,
                     GCancellable *cancel,
                     GHashTable *handled,
                     RBUriRecurseFunc func,
                     gpointer user_data)
{
    GFileEnumerator *files;
    GFileInfo *info;
    GError *error = NULL;
    GFileType file_type;
    const char *file_id;
    gboolean file_handled;
    const char *attributes =
        G_FILE_ATTRIBUTE_STANDARD_NAME ","
        G_FILE_ATTRIBUTE_STANDARD_TYPE ","
        G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN ","
        G_FILE_ATTRIBUTE_ID_FILE ","
        G_FILE_ATTRIBUTE_ACCESS_CAN_READ;

    files = g_file_enumerate_children (dir, attributes, G_FILE_QUERY_INFO_NONE, cancel, &error);
    if (error != NULL) {
        char *where;

        /* handle the case where we're given a single file to process */
        if (error->code == G_IO_ERROR_NOT_DIRECTORY) {
            g_clear_error (&error);
            info = g_file_query_info (dir, attributes, G_FILE_QUERY_INFO_NONE, cancel, &error);
            if (error == NULL) {
                if (_should_process (info)) {
                    (func) (dir, FALSE, user_data);
                }
                g_object_unref (info);
                return;
            }
        }

        where = g_file_get_uri (dir);
        rb_debug ("error enumerating %s: %s", where, error->message);
        g_free (where);
        g_error_free (error);
        return;
    }

    while (1) {
        GFile *child;
        gboolean is_dir;
        gboolean ret;

        ret = TRUE;
        info = g_file_enumerator_next_file (files, cancel, &error);
        if (error != NULL) {
            rb_debug ("error enumerating files: %s", error->message);
            break;
        } else if (info == NULL) {
            break;
        }

        if (_should_process (info) == FALSE) {
            g_object_unref (info);
            continue;
        }

        /* already handled? */
        file_id = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE);
        if (file_id == NULL) {
            /* have to hope for the best, I guess */
            file_handled = FALSE;
        } else if (g_hash_table_lookup (handled, file_id) != NULL) {
            file_handled = TRUE;
        } else {
            file_handled = FALSE;
            g_hash_table_insert (handled, g_strdup (file_id), GINT_TO_POINTER (1));
        }

        /* type? */
        file_type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
        switch (file_type) {
        case G_FILE_TYPE_DIRECTORY:
        case G_FILE_TYPE_MOUNTABLE:
            is_dir = TRUE;
            break;

        default:
            is_dir = FALSE;
            break;
        }

        if (file_handled == FALSE) {
            child = g_file_get_child (dir, g_file_info_get_name (info));
            ret = (func) (child, is_dir, user_data);

            if (is_dir) {
                _uri_handle_recurse (child, cancel, handled, func, user_data);
            }
            g_object_unref (child);
        }

        g_object_unref (info);

        if (ret == FALSE)
            break;
    }

    g_object_unref (files);
}
Exemple #6
0
int
main (int argc, char *argv[])
{
  GError *error;
  GOptionContext *context;
  GFile *source, *dest, *target;
  gboolean dest_is_dir;
  char *basename;
  int i;
  GFileCopyFlags flags;
  int retval = 0;
  gchar *param;
  gchar *summary;

  setlocale (LC_ALL, "");

  bindtextdomain (GETTEXT_PACKAGE, GVFS_LOCALEDIR);
  bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
  textdomain (GETTEXT_PACKAGE);

  error = NULL;
  param = g_strdup_printf ("%s... %s", _("SOURCE"), _("DEST"));
  summary = _("Move one or more files from SOURCE to DEST.");

  context = g_option_context_new (param);
  g_option_context_set_summary (context, summary);
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_parse (context, &argc, &argv, &error);

  if (error != NULL)
    {
      g_printerr (_("Error parsing commandline options: %s\n"), error->message);
      g_printerr ("\n");
      g_printerr (_("Try \"%s --help\" for more information."), g_get_prgname ());
      g_printerr ("\n");
      g_error_free (error);
      return 1;
    }

  if (argc <= 2)
    {
      show_help (context, _("Missing operand\n"));
      return 1;
    }

  dest = g_file_new_for_commandline_arg (argv[argc-1]);

  if (no_target_directory && argc > 3)
    {
      show_help (context, _("Too many arguments\n"));
      g_object_unref (dest);
      return 1;
    }

  dest_is_dir = is_dir (dest);

  if (!dest_is_dir && argc > 3)
    {
      g_printerr (_("Target %s is not a directory\n"), argv[argc-1]);
      show_help (context, NULL);
      g_object_unref (dest);
      return 1;
    }

  g_option_context_free (context);
  g_free (param);

  for (i = 1; i < argc - 1; i++)
    {
      source = g_file_new_for_commandline_arg (argv[i]);

      if (dest_is_dir && !no_target_directory)
	{
	  basename = g_file_get_basename (source);
	  target = g_file_get_child (dest, basename);
	  g_free (basename);
	}
      else
	target = g_object_ref (dest);

      flags = 0;
      if (backup)
	flags |= G_FILE_COPY_BACKUP;
      if (!interactive)
	flags |= G_FILE_COPY_OVERWRITE;

      error = NULL;
      if (!g_file_move (source, target, flags, NULL, progress?show_progress:NULL, NULL, &error))
	{
	  if (interactive && g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
	    {
	      char line[16];

	      g_error_free (error);
	      error = NULL;

	      basename = g_file_get_basename (target);
	      g_print ("overwrite %s?", basename);
	      g_free (basename);

	      if (fgets(line, sizeof (line), stdin) &&
		  line[0] == 'y')
		{
		  flags |= G_FILE_COPY_OVERWRITE;
		  if (!g_file_move (source, target, flags, NULL, NULL, NULL, &error))
		    goto move_failed;
		}
	    }
	  else
	    {
	    move_failed:
	      g_printerr (_("Error moving file %s: %s\n"), argv[i], error->message);
	      g_error_free (error);
	      retval = 1;
	    }
	}

      g_object_unref (source);
      g_object_unref (target);
    }

  g_object_unref (dest);

  return retval;
}
static void
visit_directory (GFile *dir, SearchThreadData *data)
{
	GFileEnumerator *enumerator;
	GFileInfo *info;
	GFile *child;
	const char *mime_type, *display_name;
	char *lower_name, *normalized;
	gboolean hit;
	int i;
	GList *l;
	const char *id;
	gboolean visited;

	enumerator = g_file_enumerate_children (dir,
						data->mime_types != NULL ?
						STD_ATTRIBUTES ","
						G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE
						:
						STD_ATTRIBUTES
						,
						0, data->cancellable, NULL);
	
	if (enumerator == NULL) {
		return;
	}

	while ((info = g_file_enumerator_next_file (enumerator, data->cancellable, NULL)) != NULL) {
		if (g_file_info_get_is_hidden (info)) {
			goto next;
		}
		
		display_name = g_file_info_get_display_name (info);
		if (display_name == NULL) {
			goto next;
		}
		
		normalized = g_utf8_normalize (display_name, -1, G_NORMALIZE_NFD);
		lower_name = g_utf8_strdown (normalized, -1);
		g_free (normalized);
		
		hit = TRUE;
		for (i = 0; data->words[i] != NULL; i++) {
			if (strstr (lower_name, data->words[i]) == NULL) {
				hit = FALSE;
				break;
			}
		}
		g_free (lower_name);
		
		if (hit && data->mime_types) {
			mime_type = g_file_info_get_content_type (info);
			hit = FALSE;
			
			for (l = data->mime_types; mime_type != NULL && l != NULL; l = l->next) {
				if (g_content_type_equals (mime_type, l->data)) {
					hit = TRUE;
					break;
				}
			}
		}
		
		child = g_file_get_child (dir, g_file_info_get_name (info));
		
		if (hit) {
			data->uri_hits = g_list_prepend (data->uri_hits, g_file_get_uri (child));
		}
		
		data->n_processed_files++;
		if (data->n_processed_files > BATCH_SIZE) {
			send_batch (data);
		}

		if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY) {
			id = g_file_info_get_attribute_string (info, G_FILE_ATTRIBUTE_ID_FILE);
			visited = FALSE;
			if (id) {
				if (g_hash_table_lookup_extended (data->visited,
								  id, NULL, NULL)) {
					visited = TRUE;
				} else {
					g_hash_table_insert (data->visited, g_strdup (id), NULL);
				}
			}
			
			if (!visited) {
				g_queue_push_tail (data->directories, g_object_ref (child));
			}
		}
		
		g_object_unref (child);
	next:
		g_object_unref (info);
	}

	g_object_unref (enumerator);
}