2007-11-22 17:03:53 +00:00
|
|
|
/*
|
|
|
|
* autosave.c - this file is part of Geany, a fast and lightweight IDE
|
|
|
|
*
|
2008-01-06 18:11:57 +00:00
|
|
|
* Copyright 2007-2008 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
|
|
|
|
* Copyright 2007-2008 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
|
2007-11-22 17:03:53 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY 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, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* $Id$
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "geany.h"
|
|
|
|
#include "support.h"
|
|
|
|
#include "document.h"
|
|
|
|
|
|
|
|
#include "plugindata.h"
|
|
|
|
#include "pluginmacros.h"
|
|
|
|
|
|
|
|
|
|
|
|
PluginFields *plugin_fields;
|
|
|
|
GeanyData *geany_data;
|
|
|
|
|
|
|
|
|
2008-03-26 17:10:18 +00:00
|
|
|
PLUGIN_VERSION_CHECK(32)
|
2007-11-22 17:03:53 +00:00
|
|
|
|
|
|
|
PLUGIN_INFO(_("Auto Save"), _("Save automatically all open files in a given time interval."),
|
|
|
|
VERSION, _("The Geany developer team"))
|
|
|
|
|
|
|
|
|
|
|
|
static gint interval;
|
|
|
|
static gboolean print_msg;
|
|
|
|
static gboolean save_all;
|
|
|
|
static guint src_id = G_MAXUINT;
|
|
|
|
static gchar *config_file;
|
|
|
|
|
|
|
|
|
|
|
|
gboolean auto_save(gpointer data)
|
|
|
|
{
|
2008-01-28 17:27:39 +00:00
|
|
|
gint cur_idx = p_document->get_cur_idx();
|
2007-11-22 17:03:53 +00:00
|
|
|
gint i, idx, max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(app->notebook));
|
|
|
|
gint saved_files = 0;
|
|
|
|
|
|
|
|
if (save_all)
|
|
|
|
{
|
|
|
|
for (i = 0; i < max; i++)
|
|
|
|
{
|
2008-01-28 17:27:39 +00:00
|
|
|
idx = p_document->get_n_idx(i);
|
2007-11-22 17:03:53 +00:00
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* skip current file to save it lastly, skip files without name */
|
2007-11-22 17:03:53 +00:00
|
|
|
if (idx != cur_idx && doc_list[idx].file_name != NULL)
|
2008-01-28 17:27:39 +00:00
|
|
|
if (p_document->save_file(idx, FALSE))
|
2007-11-22 17:03:53 +00:00
|
|
|
saved_files++;
|
|
|
|
}
|
|
|
|
}
|
2008-02-27 13:17:29 +00:00
|
|
|
/* finally save current file, do it after all other files to get correct window title and
|
|
|
|
* symbol list */
|
2007-11-22 17:03:53 +00:00
|
|
|
if (doc_list[cur_idx].file_name != NULL)
|
2008-01-28 17:27:39 +00:00
|
|
|
if (p_document->save_file(cur_idx, FALSE))
|
2007-11-22 17:03:53 +00:00
|
|
|
saved_files++;
|
|
|
|
|
|
|
|
if (saved_files > 0 && print_msg)
|
2008-04-22 14:44:45 +00:00
|
|
|
p_ui->set_statusbar(FALSE, ngettext(
|
2008-04-22 14:53:20 +00:00
|
|
|
"Autosave: Saved %d file automatically.",
|
2008-04-22 14:44:45 +00:00
|
|
|
"Autosave: Saved %d files automatically.", saved_files),
|
|
|
|
saved_files);
|
2007-11-22 17:03:53 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
void set_timeout(void)
|
2007-11-22 17:03:53 +00:00
|
|
|
{
|
|
|
|
if (src_id != G_MAXUINT)
|
|
|
|
g_source_remove(src_id);
|
|
|
|
src_id = g_timeout_add(interval * 1000, (GSourceFunc)auto_save, NULL);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void init(GeanyData *data)
|
|
|
|
{
|
|
|
|
GKeyFile *config = g_key_file_new();
|
|
|
|
GError *error = NULL;
|
|
|
|
config_file = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "plugins", G_DIR_SEPARATOR_S,
|
|
|
|
"autosave", G_DIR_SEPARATOR_S, "autosave.conf", NULL);
|
|
|
|
|
|
|
|
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
|
|
|
|
interval = g_key_file_get_integer(config, "autosave", "interval", &error);
|
|
|
|
if (error != NULL)
|
|
|
|
{
|
|
|
|
g_error_free(error);
|
|
|
|
interval = 300;
|
|
|
|
}
|
|
|
|
print_msg = g_key_file_get_boolean(config, "autosave", "print_messages", NULL);
|
|
|
|
save_all = g_key_file_get_boolean(config, "autosave", "save_all", NULL);
|
|
|
|
|
|
|
|
set_timeout();
|
|
|
|
|
|
|
|
g_key_file_free(config);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void configure(GtkWidget *parent)
|
|
|
|
{
|
|
|
|
GtkWidget *dialog, *label, *spin, *vbox, *hbox, *checkbox, *radio1, *radio2;
|
|
|
|
|
|
|
|
dialog = gtk_dialog_new_with_buttons(_("Auto Save"),
|
|
|
|
GTK_WINDOW(parent), GTK_DIALOG_DESTROY_WITH_PARENT,
|
|
|
|
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL, GTK_STOCK_OK, GTK_RESPONSE_ACCEPT, NULL);
|
2008-01-28 17:27:39 +00:00
|
|
|
vbox = p_ui->dialog_vbox_new(GTK_DIALOG(dialog));
|
2007-11-22 17:03:53 +00:00
|
|
|
gtk_widget_set_name(dialog, "GeanyDialog");
|
|
|
|
gtk_box_set_spacing(GTK_BOX(vbox), 6);
|
|
|
|
|
2008-02-06 16:02:00 +00:00
|
|
|
label = gtk_label_new(_("Auto save interval:"));
|
2007-11-22 17:03:53 +00:00
|
|
|
gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), label);
|
|
|
|
|
|
|
|
spin = gtk_spin_button_new_with_range(1, 1800, 1);
|
|
|
|
gtk_spin_button_set_value(GTK_SPIN_BUTTON(spin), interval);
|
|
|
|
|
2008-02-06 16:02:00 +00:00
|
|
|
label = gtk_label_new(_("seconds"));
|
2007-11-22 17:03:53 +00:00
|
|
|
|
|
|
|
hbox = gtk_hbox_new(FALSE, 5);
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), spin, TRUE, TRUE, 0);
|
|
|
|
gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
|
|
|
|
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 5);
|
|
|
|
|
|
|
|
checkbox = gtk_check_button_new_with_label(
|
2008-01-31 19:02:52 +00:00
|
|
|
_("Print status message if files have been automatically saved"));
|
2007-11-22 17:03:53 +00:00
|
|
|
gtk_button_set_focus_on_click(GTK_BUTTON(checkbox), FALSE);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbox), print_msg);
|
|
|
|
gtk_box_pack_start(GTK_BOX(vbox), checkbox, FALSE, FALSE, 5);
|
|
|
|
|
|
|
|
radio1 = gtk_radio_button_new_with_label(NULL,
|
|
|
|
_("Save only current open file"));
|
|
|
|
gtk_button_set_focus_on_click(GTK_BUTTON(radio1), FALSE);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), radio1);
|
|
|
|
|
|
|
|
radio2 = gtk_radio_button_new_with_label_from_widget(GTK_RADIO_BUTTON(radio1),
|
|
|
|
_("Save all open files"));
|
|
|
|
gtk_button_set_focus_on_click(GTK_BUTTON(radio2), FALSE);
|
|
|
|
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(radio2), save_all);
|
|
|
|
gtk_container_add(GTK_CONTAINER(vbox), radio2);
|
|
|
|
|
|
|
|
gtk_widget_show_all(vbox);
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
/* run the dialog and check for the response code */
|
2007-11-22 17:03:53 +00:00
|
|
|
if (gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT)
|
|
|
|
{
|
|
|
|
GKeyFile *config = g_key_file_new();
|
|
|
|
gchar *data;
|
|
|
|
gchar *config_dir = g_path_get_dirname(config_file);
|
|
|
|
|
|
|
|
interval = gtk_spin_button_get_value_as_int((GTK_SPIN_BUTTON(spin)));
|
|
|
|
print_msg = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbox));
|
|
|
|
save_all = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(radio2));
|
|
|
|
|
|
|
|
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
|
|
|
|
|
|
|
|
g_key_file_set_integer(config, "autosave", "interval", interval);
|
|
|
|
g_key_file_set_boolean(config, "autosave", "print_messages", print_msg);
|
|
|
|
g_key_file_set_boolean(config, "autosave", "save_all", save_all);
|
|
|
|
|
2008-01-28 17:27:39 +00:00
|
|
|
if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && p_utils->mkdir(config_dir, TRUE) != 0)
|
2007-11-22 17:03:53 +00:00
|
|
|
{
|
2008-01-28 17:27:39 +00:00
|
|
|
p_dialogs->show_msgbox(GTK_MESSAGE_ERROR,
|
2007-11-22 17:03:53 +00:00
|
|
|
_("Plugin configuration directory could not be created."));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-02-27 13:17:29 +00:00
|
|
|
/* write config to file */
|
2007-11-22 17:03:53 +00:00
|
|
|
data = g_key_file_to_data(config, NULL, NULL);
|
2008-01-28 17:27:39 +00:00
|
|
|
p_utils->write_file(config_file, data);
|
2007-11-22 17:03:53 +00:00
|
|
|
g_free(data);
|
|
|
|
}
|
|
|
|
|
2008-02-27 13:17:29 +00:00
|
|
|
set_timeout(); /* apply the changes */
|
2007-11-22 17:03:53 +00:00
|
|
|
|
|
|
|
g_free(config_dir);
|
|
|
|
g_key_file_free(config);
|
|
|
|
}
|
|
|
|
gtk_widget_destroy(dialog);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-20 11:24:23 +00:00
|
|
|
void cleanup(void)
|
2007-11-22 17:03:53 +00:00
|
|
|
{
|
|
|
|
g_source_remove(src_id);
|
|
|
|
g_free(config_file);
|
|
|
|
}
|