1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
/* $Id: install_gui.c,v 1.24 2008/05/25 09:46:20 rousseau Exp $ */
/*******************************************************************************
* install_gui.c
* A module of J-Pilot http://jpilot.org
*
* Copyright (C) 1999-2002 by Judd Montgomery
*
* 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; version 2 of the License.
*
* 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
******************************************************************************/
#include "config.h"
#include "i18n.h"
#include <gtk/gtk.h>
#include <string.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#include "utils.h"
#include "prefs.h"
#include "log.h"
static int update_clist();
static GtkWidget *clist;
static int line_selected;
static GtkWidget *filew=NULL;
/* */
/*file must not be open elsewhere when this is called */
/*the first line is 0 */
int install_remove_line(int deleted_line)
{
FILE *in;
FILE *out;
char line[1002];
char *Pc;
int r, line_count;
in = jp_open_home_file(EPN"_to_install", "r");
if (!in) {
jp_logf(JP_LOG_DEBUG, "failed opening install_file\n");
return EXIT_FAILURE;
}
out = jp_open_home_file(EPN"_to_install.tmp", "w");
if (!out) {
fclose(in);
jp_logf(JP_LOG_DEBUG, "failed opening install_file.tmp\n");
return EXIT_FAILURE;
}
for (line_count=0; (!feof(in)); line_count++) {
line[0]='\0';
Pc = fgets(line, 1000, in);
if (!Pc) {
break;
}
if (line_count == deleted_line) {
continue;
}
r = fprintf(out, "%s", line);
if (r==EOF) {
break;
}
}
fclose(in);
fclose(out);
rename_file(EPN"_to_install.tmp", EPN"_to_install");
return EXIT_SUCCESS;
}
int install_append_line(const char *line)
{
FILE *out;
int r;
out = jp_open_home_file(EPN"_to_install", "a");
if (!out) {
return EXIT_FAILURE;
}
r = fprintf(out, "%s\n", line);
if (r==EOF) {
fclose(out);
return EXIT_FAILURE;
}
fclose(out);
return EXIT_SUCCESS;
}
/*---------- */
static gboolean cb_destroy(GtkWidget *widget)
{
filew = NULL;
gtk_main_quit();
return TRUE;
}
static void
cb_quit(GtkWidget *widget,
gpointer data)
{
const char *sel;
char dir[MAX_PREF_VALUE+2];
int i;
jp_logf(JP_LOG_DEBUG, "Quit\n");
sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));
g_strlcpy(dir, sel, sizeof(dir));
i=strlen(dir)-1;
if (i<0) i=0;
if (dir[i]!='/') {
for (i=strlen(dir); i>=0; i--) {
if (dir[i]=='/') {
dir[i+1]='\0';
break;
}
}
}
set_pref(PREF_INSTALL_PATH, 0, dir, TRUE);
filew = NULL;
gtk_widget_destroy(data);
}
static void
cb_add(GtkWidget *widget,
gpointer data)
{
const char *sel;
struct stat statb;
jp_logf(JP_LOG_DEBUG, "Add\n");
sel = gtk_file_selection_get_filename(GTK_FILE_SELECTION(data));
jp_logf(JP_LOG_DEBUG, "file selected [%s]\n", sel);
/*Check to see if its a regular file */
if (stat(sel, &statb)) {
jp_logf(JP_LOG_DEBUG, "File selected was not stat-able\n");
return;
}
if (!S_ISREG(statb.st_mode)) {
jp_logf(JP_LOG_DEBUG, "File selected was not a regular file\n");
return;
}
install_append_line(sel);
update_clist();
}
static void
cb_remove(GtkWidget *widget,
gpointer data)
{
if (line_selected < 0) {
return;
}
jp_logf(JP_LOG_DEBUG, "Remove line %d\n", line_selected);
install_remove_line(line_selected);
update_clist();
}
static int
update_clist()
{
FILE *in;
char line[1002];
char *Pc;
char *new_line[2];
int kept_line_selected;
int count;
int len;
new_line[0]=line;
new_line[1]=NULL;
kept_line_selected = line_selected;
in = jp_open_home_file(EPN"_to_install", "r");
if (!in) {
return EXIT_FAILURE;
}
gtk_clist_freeze(GTK_CLIST(clist));
gtk_clist_clear(GTK_CLIST(clist));
#ifdef __APPLE__
gtk_clist_thaw(GTK_CLIST(clist));
gtk_widget_hide(clist);
gtk_widget_show_all(clist);
gtk_clist_freeze(GTK_CLIST(clist));
#endif
for (count=0; (!feof(in)); count++) {
line[0]='\0';
Pc = fgets(line, 1000, in);
if (!Pc) {
break;
}
len=strlen(line);
if ((line[len-1]=='\n') || (line[len-1]=='\r')) line[len-1]='\0';
if ((line[len-2]=='\n') || (line[len-2]=='\r')) line[len-2]='\0';
gtk_clist_append(GTK_CLIST(clist), new_line);
}
if (kept_line_selected > count -1) {
kept_line_selected = count - 1;
}
clist_select_row(GTK_CLIST(clist), kept_line_selected, 0);
fclose(in);
gtk_clist_thaw(GTK_CLIST(clist));
return EXIT_SUCCESS;
}
static void cb_clist_selection(GtkWidget *clist,
gint row,
gint column,
GdkEventButton *event,
gpointer data)
{
/*printf("row = %d\n", row); */
line_selected = row;
return;
}
int install_gui(GtkWidget *main_window, int w, int h, int x, int y)
{
GtkWidget *scrolled_window;
GtkWidget *button;
GtkWidget *label;
char temp[256];
const char *svalue;
gchar *titles[2];
if (filew) {
return EXIT_SUCCESS;
}
line_selected = -1;
g_snprintf(temp, sizeof(temp), "%s %s", PN, _("Install"));
filew = gtk_widget_new(GTK_TYPE_FILE_SELECTION,
"type", GTK_WINDOW_TOPLEVEL,
"title", temp,
NULL);
gtk_window_set_default_size(GTK_WINDOW(filew), w, h);
gtk_widget_set_uposition(filew, x, y);
gtk_window_set_modal(GTK_WINDOW(filew), TRUE);
gtk_window_set_transient_for(GTK_WINDOW(filew), GTK_WINDOW(main_window));
get_pref(PREF_INSTALL_PATH, NULL, &svalue);
if (svalue && svalue[0]) {
gtk_file_selection_set_filename(GTK_FILE_SELECTION(filew), svalue);
}
gtk_file_selection_hide_fileop_buttons((gpointer) filew);
gtk_widget_hide((GTK_FILE_SELECTION(filew)->cancel_button));
gtk_signal_connect(GTK_OBJECT(filew), "destroy",
GTK_SIGNAL_FUNC(cb_destroy), filew);
/*Even though I hide the ok button I still want to connect its signal */
/*because a double click on the file name also calls this callback */
gtk_widget_hide(GTK_WIDGET(GTK_FILE_SELECTION(filew)->ok_button));
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(filew)->ok_button),
"clicked", GTK_SIGNAL_FUNC(cb_add), filew);
/* i18n hack to have title correctly translated */
titles[0] = strdup(_("Files to be installed"));
titles[1]=NULL;
clist = gtk_clist_new_with_titles(1, titles);
if (titles[0]) free(titles[0]);
gtk_widget_set_usize(GTK_WIDGET(clist), 0, 166);
gtk_clist_column_titles_passive(GTK_CLIST(clist));
gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_BROWSE);
/*Add the scrolled window */
scrolled_window = gtk_scrolled_window_new(NULL, NULL);
gtk_container_add(GTK_CONTAINER(scrolled_window), GTK_WIDGET(clist));
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window),
GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
gtk_container_set_border_width(GTK_CONTAINER(scrolled_window), 5);
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->action_area),
scrolled_window, TRUE, TRUE, 0);
gtk_signal_connect(GTK_OBJECT(clist), "select_row",
GTK_SIGNAL_FUNC(cb_clist_selection), NULL);
gtk_widget_show(clist);
gtk_widget_show(scrolled_window);
label = gtk_label_new(_("To change to a hidden directory type it below and hit TAB"));
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->main_vbox),
label, FALSE, FALSE, 0);
gtk_widget_show(label);
#ifdef ENABLE_GTK2
button = gtk_button_new_from_stock(GTK_STOCK_ADD);
#else
button = gtk_button_new_with_label(_("Add"));
#endif
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->ok_button->parent),
button, TRUE, TRUE, 0);
gtk_signal_connect(GTK_OBJECT(button),
"clicked", GTK_SIGNAL_FUNC(cb_add), filew);
gtk_widget_show(button);
#ifdef ENABLE_GTK2
button = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
#else
button = gtk_button_new_with_label(_("Remove"));
#endif
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->ok_button->parent),
button, TRUE, TRUE, 0);
gtk_signal_connect(GTK_OBJECT(button),
"clicked", GTK_SIGNAL_FUNC(cb_remove), filew);
gtk_widget_show(button);
#ifdef ENABLE_GTK2
button = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
#else
button = gtk_button_new_with_label(_("Close"));
#endif
gtk_box_pack_start(GTK_BOX(GTK_FILE_SELECTION(filew)->ok_button->parent),
button, TRUE, TRUE, 0);
gtk_signal_connect(GTK_OBJECT(button),
"clicked", GTK_SIGNAL_FUNC(cb_quit), filew);
gtk_widget_show(button);
gtk_widget_show(filew);
update_clist();
gtk_main();
return EXIT_SUCCESS;
}
|