Example #1
0
void
sourceview_io_save_as (SourceviewIO* sio, GFile* file)
{
	AnjutaShell* shell = ANJUTA_PLUGIN (sio->sv->priv->plugin)->shell;
	gboolean backup = TRUE;
	gsize len;

	g_return_if_fail (file != NULL);

	cancel_monitor (sio);

	backup = g_settings_get_boolean (sio->sv->priv->settings,
	                                 "backup");

	if (sio->last_encoding == NULL)
	{
		sio->write_buffer = ianjuta_editor_get_text_all (IANJUTA_EDITOR(sio->sv),
														 NULL);
		len = strlen (sio->write_buffer);
	}
	else
	{
		GError* err = NULL;
		gchar* buffer_text = ianjuta_editor_get_text_all (IANJUTA_EDITOR(sio->sv),
														  NULL);
		sio->write_buffer = anjuta_convert_from_utf8 (buffer_text,
													  -1,
													  sio->last_encoding,
													  &len,
													  &err);
		g_free (buffer_text);
		if (err != NULL)
		{
			g_signal_emit_by_name (sio, "save-failed", err);
			g_error_free(err);
			return;
		}
	}
	g_cancellable_reset (sio->cancel);
	g_file_replace_contents_async (file,
	                               sio->write_buffer,
	                               len,
	                               NULL,
	                               backup,
	                               G_FILE_CREATE_NONE,
	                               sio->cancel,
	                               on_save_finished,
	                               sio);
	anjuta_shell_saving_push (shell);

	if (sio->file != file)
	{
		if (sio->file)
			g_object_unref (sio->file);
		sio->file = file;
		g_object_ref (file);
	}
	g_object_ref (sio);
}
/* This function is asynchronous .. slightly worried about re-entrancy here */
static void
mex_queue_model_save (MexQueueModel *model)
{
  GFile *f;
  gchar *filename;
  JsonGenerator *generator;
  gchar *buf;
  gsize buf_len;

  filename = _queue_file_name ();
  f = g_file_new_for_path (filename);

  if (mex_model_get_length (MEX_MODEL (model)) == 0)
    {
      GError *error = NULL;

      if (!g_file_delete (f, NULL, &error))
        {
          g_warning (G_STRLOC ": Unable to delete file: %s",
                     error->message);
          g_clear_error (&error);
        }

      g_object_unref (f);
      g_free (filename);
      return;
    }

  generator = _model_to_generator (model);

  buf = json_generator_to_data (generator, &buf_len);

  g_file_replace_contents_async (f,
                                 buf,
                                 buf_len,
                                 NULL,
                                 FALSE,
                                 G_FILE_CREATE_REPLACE_DESTINATION,
                                 NULL,
                                 (GAsyncReadyCallback)_file_replaced_cb,
                                 buf);

  g_object_unref (f);
  g_free (filename);
  g_object_unref (generator);
}
G_MODULE_EXPORT void
on_ssh_export_button_clicked (GtkWidget *widget, SeahorseWidget *swidget)
{
	SeahorseSource *sksrc;
	SeahorseObject *object;
	GFile *file;
	GtkDialog *dialog;
	guchar *results;
	gsize n_results;
	gchar* uri = NULL;
	GError *err = NULL;

	object = SEAHORSE_OBJECT_WIDGET (swidget)->object;
	g_return_if_fail (SEAHORSE_IS_SSH_KEY (object));
	sksrc = seahorse_object_get_source (object);
	g_return_if_fail (SEAHORSE_IS_SSH_SOURCE (sksrc));
	
	dialog = seahorse_util_chooser_save_new (_("Export Complete Key"), 
	                                         GTK_WINDOW (seahorse_widget_get_toplevel (swidget)));
	seahorse_util_chooser_show_key_files (dialog);
	seahorse_util_chooser_set_filename (dialog, object);

	uri = seahorse_util_chooser_save_prompt (dialog);
	if (!uri) 
		return;
	
	results = seahorse_ssh_source_export_private (SEAHORSE_SSH_SOURCE (sksrc), 
	                                              SEAHORSE_SSH_KEY (object),
	                                              &n_results, &err);
	
	if (results) {
		g_return_if_fail (err == NULL);
		file = g_file_new_for_uri (uri);
		g_file_replace_contents_async (file, (gchar*)results, n_results, NULL, FALSE, 
		                               G_FILE_CREATE_PRIVATE, NULL, 
		                               (GAsyncReadyCallback)export_complete, results);
	}
	
	if (err) {
	        seahorse_util_handle_error (err, _("Couldn't export key."));
	        g_clear_error (&err);
	}
	
	g_free (uri);
}
Example #4
0
void buffer_to_txt (Ebook * ebook)
{
	GtkTextBuffer * buffer;
	GtkTextIter start, end;
	gboolean exists;
	gchar * text;

	g_return_if_fail (ebook);
	g_return_if_fail (ebook->filename);
	buffer = GTK_TEXT_BUFFER(gtk_builder_get_object (ebook->builder, "textbuffer1"));
	gtk_text_buffer_get_bounds (buffer, &start, &end);
	text = g_strdup(gtk_text_buffer_get_text (buffer, &start, &end, TRUE));
	ebook->gfile = g_file_new_for_path (ebook->filename);
	exists = g_file_query_exists (ebook->gfile, NULL);
	if (!exists)
		g_file_set_contents (ebook->filename, " ", -1, NULL);
	g_file_replace_contents_async (ebook->gfile, text, strlen(text), NULL, FALSE, 0,
		NULL, async_cb, ebook);
}
Example #5
0
/**
 * e_file_replace_contents_async:
 * @file: input #GFile
 * @contents: string of contents to replace the file with
 * @length: the length of @contents in bytes
 * @etag: a new entity tag for the @file, or %NULL
 * @make_backup: %TRUE if a backup should be created
 * @flags: a set of #GFileCreateFlags
 * @callback: a #GAsyncReadyCallback to call when the request is satisfied
 * @user_data: the data to pass to the callback function
 *
 * This is a wrapper for g_file_replace_contents_async() that also returns
 * an #EActivity to track the file operation.  Cancelling the activity will
 * cancel the file operation.  See g_file_replace_contents_async() for more
 * details.
 *
 * Returns: an #EActivity for the file operation
 **/
EActivity *
e_file_replace_contents_async (GFile *file,
                               const gchar *contents,
                               gsize length,
                               const gchar *etag,
                               gboolean make_backup,
                               GFileCreateFlags flags,
                               GAsyncReadyCallback callback,
                               gpointer user_data)
{
	GSimpleAsyncResult *simple;
	GCancellable *cancellable;
	AsyncContext *context;
	const gchar *format;
	gchar *description;
	gchar *basename;
	gchar *filename;
	gchar *hostname;
	gchar *uri;

	g_return_val_if_fail (G_IS_FILE (file), NULL);
	g_return_val_if_fail (contents != NULL, NULL);

	uri = g_file_get_uri (file);
	filename = g_filename_from_uri (uri, &hostname, NULL);
	if (filename != NULL)
		basename = g_filename_display_basename (filename);
	else
		basename = g_strdup (_("(Unknown Filename)"));

	if (hostname == NULL) {
		/* Translators: The string value is the basename of a file. */
		format = _("Writing \"%s\"");
		description = g_strdup_printf (format, basename);
	} else {
		/* Translators: The first string value is the basename of a
		 * remote file, the second string value is the hostname. */
		format = _("Writing \"%s\" to %s");
		description = g_strdup_printf (format, basename, hostname);
	}

	cancellable = g_cancellable_new ();

	context = g_slice_new0 (AsyncContext);
	context->activity = e_activity_new ();

	e_activity_set_text (context->activity, description);
	e_activity_set_cancellable (context->activity, cancellable);

	simple = g_simple_async_result_new (
		G_OBJECT (file), callback, user_data,
		e_file_replace_contents_async);

	g_simple_async_result_set_op_res_gpointer (
		simple, context, (GDestroyNotify) async_context_free);

	g_file_replace_contents_async (
		file, contents, length, etag,
		make_backup, flags, cancellable,
		(GAsyncReadyCallback) file_replace_contents_cb,
		simple);

	g_object_unref (cancellable);

	g_free (description);
	g_free (basename);
	g_free (filename);
	g_free (hostname);
	g_free (uri);

	return context->activity;
}