"Fossies" - the Fresh Open Source Software Archive 
Member "jpilot-2_0_1/restore_gui.c" (3 Apr 2021, 15482 Bytes) of package /linux/privat/jpilot-2_0_1.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*******************************************************************************
2 * restore_gui.c
3 * A module of J-Pilot http://jpilot.org
4 *
5 * Copyright (C) 2001-2014 by Judd Montgomery
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
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.
15 *
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ******************************************************************************/
20
21 /********************************* Includes ***********************************/
22 #include "config.h"
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <dirent.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <unistd.h>
30 #include <gtk/gtk.h>
31
32 #include "i18n.h"
33 #include "utils.h"
34 #include "prefs.h"
35 #include "sync.h"
36 #include "log.h"
37 #include "restore.h"
38
39 /******************************* Global vars **********************************/
40 static GtkWidget *user_entry;
41 static GtkWidget *user_id_entry;
42 static GtkWidget *restoreTreeView;
43
44 enum {
45 RESTORE_DISPLAY_COLUMN_ENUM = 0,
46 RESTORE_DATA_COLUMN_ENUM,
47 RESTORE_NUM_COLS
48 };
49
50 gboolean
51 checkForDuplication (GtkTreeModel *model,
52 GtkTreePath *path,
53 GtkTreeIter *iter,
54 gpointer data);
55
56 /****************************** Main Code *************************************/
57 static gboolean cb_restore_destroy(GtkWidget *widget) {
58 gtk_main_quit();
59
60 return FALSE;
61 }
62
63 static void cb_restore_ok(GtkWidget *widget, gpointer data) {
64 GList *list, *temp_list;
65
66 char *text;
67 char file[FILENAME_MAX], backup_file[FILENAME_MAX];
68 char home_dir[FILENAME_MAX];
69 struct stat buf, backup_buf;
70 int r1, r2;
71
72 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(restoreTreeView));
73 list = gtk_tree_selection_get_selected_rows(gtk_tree_view_get_selection(GTK_TREE_VIEW(restoreTreeView)),&model);
74
75 get_home_file_name("", home_dir, sizeof(home_dir));
76
77 /* Remove anything that was supposed to be installed */
78 g_snprintf(file, sizeof(file), "%s/"EPN".install", home_dir);
79 unlink(file);
80
81 jp_logf(JP_LOG_WARN, "%s%s%s\n", "-----===== ", _("Restore Handheld"), " ======-----");
82
83 for (temp_list = list; temp_list; temp_list = temp_list->next) {
84 GtkTreePath * path = temp_list->data;
85 GtkTreeIter iter;
86 if(gtk_tree_model_get_iter(model,&iter,path)) {
87 gtk_tree_model_get(model, &iter, RESTORE_DISPLAY_COLUMN_ENUM, &text, -1);
88 int * i = gtk_tree_path_get_indices ( path ) ;
89 jp_logf(JP_LOG_DEBUG, "row %ld [%s]\n", i[0], text);
90 /* Look for the file in the JPILOT_HOME and JPILOT_HOME/backup.
91 * Restore the newest modified date one, or the only one. */
92 g_snprintf(file, sizeof(file), "%s/%s", home_dir, text);
93 g_snprintf(backup_file, sizeof(backup_file), "%s/backup/%s", home_dir, text);
94 r1 = !stat(file, &buf);
95 r2 = !stat(backup_file, &backup_buf);
96 if (r1 && r2) {
97 /* found in JPILOT_HOME and JPILOT_HOME/backup */
98 if (buf.st_mtime > backup_buf.st_mtime) {
99 jp_logf(JP_LOG_DEBUG, "Restore: found in home and backup, using home file %s\n", text);
100 install_append_line(file);
101 } else {
102 jp_logf(JP_LOG_DEBUG, "Restore: found in home and backup, using home/backup file %s\n", text);
103 install_append_line(backup_file);
104 }
105 } else if (r1) {
106 /* only found in JPILOT_HOME */
107 install_append_line(file);
108 jp_logf(JP_LOG_DEBUG, "Restore: using home file %s\n", text);
109 } else if (r2) {
110 /* only found in JPILOT_HOME/backup */
111 jp_logf(JP_LOG_DEBUG, "Restore: using home/backup file %s\n", text);
112 install_append_line(backup_file);
113 }
114 }
115 }
116
117 setup_sync(SYNC_NO_PLUGINS | SYNC_OVERRIDE_USER | SYNC_RESTORE);
118
119 gtk_widget_destroy(data);
120 }
121
122 static void cb_restore_quit(GtkWidget *widget, gpointer data) {
123 gtk_widget_destroy(data);
124 }
125
126
127 /*
128 * path is the dir to open
129 * check_for_dups will check the listStore and not add if its a duplicate
130 * check_exts will not add if its not a pdb, prc, or pqa.
131 */
132 static int populate_listStore_subpath(char *path, int check_for_dups, int check_exts) {
133 char *row_text[1];
134 GtkListStore *listStore = GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(restoreTreeView)));
135 GtkTreeIter iter;
136 DIR *dir;
137 struct dirent *dirent;
138 char last4[8];
139 int i, num, len, found;
140
141 jp_logf(JP_LOG_DEBUG, "opening dir %s\n", path);
142 dir = opendir(path);
143 num = 0;
144 if (!dir) {
145 jp_logf(JP_LOG_DEBUG, "opening dir failed\n");
146 } else {
147 for (i = 0; (dirent = readdir(dir)); i++) {
148 if (i > 1000) {
149 jp_logf(JP_LOG_WARN, "populate_listStore_subpath(): %s\n", _("infinite loop"));
150 closedir(dir);
151 return EXIT_FAILURE;
152 }
153 if (dirent->d_name[0] == '.') {
154 continue;
155 }
156 if (!strncmp(dirent->d_name, "Unsaved Preferences", 17)) {
157 jp_logf(JP_LOG_DEBUG, "skipping %s\n", dirent->d_name);
158 continue;
159 }
160 if (check_exts) {
161 len = strlen(dirent->d_name);
162 if (len < 4) {
163 continue;
164 }
165 strncpy(last4, dirent->d_name + len - 4, 4);
166 last4[4] = '\0';
167 if (strcmp(last4, ".pdb") &&
168 strcmp(last4, ".prc") &&
169 strcmp(last4, ".pqa")) {
170 continue;
171 }
172 }
173
174 if (check_for_dups) {
175 found = 0;
176 RestoreDataSearchElement * element = malloc(sizeof(RestoreDataSearchElement));;
177 element ->found = 0;
178 element ->textToSearch = dirent->d_name;
179 gtk_tree_model_foreach(GTK_TREE_MODEL(listStore), checkForDuplication, element);
180 found = element -> found;
181 g_free(element);
182 if (found == 1)
183 continue;
184
185 }
186
187 row_text[0] = dirent->d_name;
188 {
189 gchar *utf8_text;
190
191 utf8_text = g_locale_to_utf8(row_text[0], -1, NULL, NULL, NULL);
192 if (!utf8_text) {
193 jp_logf(JP_LOG_GUI, _("Unable to convert filename for GTK display\n"));
194 jp_logf(JP_LOG_GUI, _("See console log to find which file will not be restored\n"));
195 jp_logf(JP_LOG_STDOUT | JP_LOG_FILE, _("Unable to convert filename for GTK display\n"));
196 jp_logf(JP_LOG_STDOUT | JP_LOG_FILE, _("File %s will not be restored\n"), row_text[0]);
197 continue;
198 }
199 row_text[0] = utf8_text;
200 gtk_list_store_append(listStore,&iter);
201 gtk_list_store_set(listStore,&iter,RESTORE_DISPLAY_COLUMN_ENUM,row_text[0],-1);
202 g_free(utf8_text);
203 }
204 num++;
205 }
206 closedir(dir);
207 }
208
209
210 return num;
211 }
212
213 static int populate_listStore(void) {
214 char path[FILENAME_MAX];
215
216 get_home_file_name("backup", path, sizeof(path));
217 cleanup_path(path);
218 populate_listStore_subpath(path, 0, 0);
219
220 get_home_file_name("", path, sizeof(path));
221 cleanup_path(path);
222 //populate_listStore_subpath(path, 1, 1);
223 populate_listStore_subpath(path, 1, 1);
224 gtk_tree_selection_select_all(gtk_tree_view_get_selection(GTK_TREE_VIEW(restoreTreeView)));
225
226 return EXIT_SUCCESS;
227 }
228 gboolean
229 checkForDuplication (GtkTreeModel *model,
230 GtkTreePath *path,
231 GtkTreeIter *iter,
232 gpointer data) {
233 char * rowText;
234 RestoreDataSearchElement *element = data;
235 gtk_tree_model_get(model,iter,RESTORE_DISPLAY_COLUMN_ENUM,&rowText,-1);
236 if (data != NULL) {
237 if ((strcmp(element->textToSearch, rowText))) {
238 element->found = 1;
239 return TRUE;
240 }
241 }
242 return FALSE;
243
244 }
245
246 int restore_gui(GtkWidget *main_window, int w, int h, int x, int y) {
247 GtkWidget *restore_window;
248 GtkWidget *button;
249 GtkWidget *vbox;
250 GtkWidget *hbox;
251 GtkWidget *scrolled_window;
252 GtkWidget *label;
253 GtkListStore *listStore;
254 const char *svalue;
255 long ivalue;
256 long char_set;
257 char str_int[20];
258
259 jp_logf(JP_LOG_DEBUG, "restore_gui()\n");
260
261 restore_window = gtk_widget_new(GTK_TYPE_WINDOW,
262 "type", GTK_WINDOW_TOPLEVEL,
263 "title", _("Restore Handheld"),
264 NULL);
265
266 gtk_window_set_default_size(GTK_WINDOW(restore_window), w, h);
267 //gtk_widget_set_uposition(restore_window, x, y);
268 gtk_container_set_border_width(GTK_CONTAINER(restore_window), 5);
269 gtk_window_set_default_size(GTK_WINDOW(restore_window), w, h);
270 gtk_window_set_modal(GTK_WINDOW(restore_window), TRUE);
271 gtk_window_set_transient_for(GTK_WINDOW(restore_window), GTK_WINDOW(main_window));
272
273 g_signal_connect(G_OBJECT(restore_window), "destroy",
274 G_CALLBACK(cb_restore_destroy), restore_window);
275
276 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
277 gtk_container_add(GTK_CONTAINER(restore_window), vbox);
278
279 /* Label for instructions */
280 label = gtk_label_new(_("To restore your handheld:"));
281 gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
282 gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
283 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
284 label = gtk_label_new(_("1. Choose the applications you wish to restore. The default is all."));
285 gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
286 gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
287 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
288 label = gtk_label_new(_("2. Enter the User Name and User ID."));
289 gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
290 gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
291 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
292 label = gtk_label_new(_("3. Press the OK button."));
293 gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
294 gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
295 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
296 label = gtk_label_new(_("This will overwrite data that is currently on the handheld."));
297 gtk_widget_set_halign(GTK_WIDGET(label), GTK_ALIGN_START);
298 gtk_widget_set_valign(GTK_WIDGET(label), GTK_ALIGN_START);
299 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
300
301 /* List of files to restore */
302 scrolled_window = gtk_scrolled_window_new(NULL, NULL);
303 gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 0);
304 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
305 GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
306 gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
307 listStore = gtk_list_store_new(2, G_TYPE_STRING, G_TYPE_POINTER);
308 restoreTreeView = gtk_tree_view_new_with_model(GTK_TREE_MODEL(listStore));
309 GtkCellRenderer *textRenderer = gtk_cell_renderer_text_new();
310 GtkTreeViewColumn *textColumn = gtk_tree_view_column_new_with_attributes("",
311 textRenderer,
312 "text", RESTORE_DISPLAY_COLUMN_ENUM,
313 NULL);
314 gtk_tree_view_column_set_clickable(textColumn, gtk_false());
315 gtk_tree_view_column_set_sizing(textColumn, GTK_TREE_VIEW_COLUMN_AUTOSIZE);
316 gtk_tree_selection_set_mode(gtk_tree_view_get_selection(GTK_TREE_VIEW(restoreTreeView)),
317 GTK_SELECTION_MULTIPLE);
318 gtk_tree_view_insert_column(GTK_TREE_VIEW(restoreTreeView),textColumn,RESTORE_DISPLAY_COLUMN_ENUM);
319 gtk_tree_view_column_set_sort_column_id(textColumn, RESTORE_DISPLAY_COLUMN_ENUM);
320 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(restoreTreeView), gtk_false());
321 gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(listStore), RESTORE_DISPLAY_COLUMN_ENUM, GTK_SORT_ASCENDING);
322
323 gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(restoreTreeView));
324 g_object_unref(listStore);
325 /* User entry */
326 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
327 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
328 label = gtk_label_new(_("User Name"));
329 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
330 user_entry = gtk_entry_new();
331 gtk_entry_set_max_length(GTK_ENTRY(user_entry), 126);
332 entry_set_multiline_truncate(GTK_ENTRY(user_entry), TRUE);
333 get_pref(PREF_USER, NULL, &svalue);
334 if ((svalue) && (svalue[0])) {
335 /* Convert User Name stored in Palm character set */
336 char user_name[128];
337
338 get_pref(PREF_CHAR_SET, &char_set, NULL);
339 g_strlcpy(user_name, svalue, 128);
340 charset_p2j(user_name, 128, char_set);
341 gtk_entry_set_text(GTK_ENTRY(user_entry), user_name);
342 }
343 gtk_box_pack_start(GTK_BOX(hbox), user_entry, TRUE, TRUE, 0);
344
345 /* User ID entry */
346 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
347 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
348 label = gtk_label_new(_("User ID"));
349 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
350 user_id_entry = gtk_entry_new();
351 gtk_entry_set_max_length(GTK_ENTRY(user_id_entry), 10);
352 entry_set_multiline_truncate(GTK_ENTRY(user_id_entry), TRUE);
353 get_pref(PREF_USER_ID, &ivalue, NULL);
354 sprintf(str_int, "%ld", ivalue);
355 gtk_entry_set_text(GTK_ENTRY(user_id_entry), str_int);
356 gtk_box_pack_start(GTK_BOX(hbox), user_id_entry, TRUE, TRUE, 0);
357
358 /* Cancel/OK buttons */
359 hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
360 gtk_container_set_border_width(GTK_CONTAINER(hbox), 12);
361 gtk_button_box_set_layout(GTK_BUTTON_BOX (hbox), GTK_BUTTONBOX_END);
362 gtk_box_set_spacing(GTK_BOX(hbox), 6);
363 gtk_box_pack_start(GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
364
365 button = gtk_button_new_with_label("Cancel");
366 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
367 g_signal_connect(G_OBJECT(button), "clicked",
368 G_CALLBACK(cb_restore_quit), restore_window);
369
370 button = gtk_button_new_with_label("OK");
371 gtk_box_pack_start(GTK_BOX(hbox), button, TRUE, TRUE, 0);
372 g_signal_connect(G_OBJECT(button), "clicked",
373 G_CALLBACK(cb_restore_ok), restore_window);
374
375 populate_listStore();
376
377 gtk_widget_show_all(restore_window);
378
379 gtk_main();
380
381 return EXIT_SUCCESS;
382 }
383