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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
/*
* Copyright (C) 2006, Magnus Hjorth
*
* This file is part of mhWaveEdit.
*
* mhWaveEdit 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.
*
* mhWaveEdit 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 mhWaveEdit; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
*/
#include <config.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <unistd.h>
#include "session.h"
#include "inifile.h"
#include "um.h"
#include "tempfile.h"
#include "gettext.h"
#include "document.h"
#define SESSION_RUNNING 0
#define SESSION_SUSPENDED 1
#define SESSION_CRASHED 2
#define SESSION_LEFTOVER 3
#define SESSION_OLD 4
#define SESSION_UNKNOWN 5
struct session {
int id;
int pid;
int state;
off_t size;
time_t date;
gchar *logfile;
GList *datafiles;
};
static int current_id = 0;
static gchar *current_filename;
static EFILE *current_file;
static GList *session_list = NULL;
/* static EFILE *current_session = NULL; */
static gint session_compare_func(gconstpointer a, gconstpointer b)
{
struct session *as = (struct session *)a, *bs = (struct session *)b;
return (as->date > bs->date)?1:0;
}
void session_init(int *argc, char **argv)
{
int highest_id = 0;
gchar *session_dir,*c,*p,*q;
DIR *d;
struct dirent *de;
int i,j,k,l,m,n;
char r;
struct session *s;
struct stat st;
GList *list,*list2;
gboolean b;
/* Check for session files on the system */
session_dir = g_strjoin(NULL,get_home_directory(),"/.mhwaveedit",NULL);
d = opendir(session_dir);
if (d == NULL) {
user_perror(_("Error opening session directory"));
return;
}
while (1) {
de = readdir(d);
if (de == NULL) break;
i = sscanf(de->d_name,"mhwaveedit-session-%d-%d-%c",&j,&k,&r);
if (i < 3) continue;
/* Stat the session file once to check mod. date and size */
p = g_strdup_printf("%s/%s",session_dir,de->d_name);
i = stat(p,&st);
if (i < 0) {
console_perror(p);
g_free(p);
continue;
} else if (!S_ISREG(st.st_mode)) {
q = g_strdup_printf(_("%s: Wrong file type"),p);
console_message(q);
g_free(q);
g_free(p);
continue;
}
s = g_malloc(sizeof(*s));
s->id = k;
if (k > highest_id) highest_id = k;
s->pid = j;
s->datafiles = NULL;
s->size = st.st_size;
s->date = st.st_mtime;
s->logfile = p;
/* Choose state */
if (r == 's') {
s->state = SESSION_SUSPENDED;
} else {
/* Is there a process with the PID running? */
i = kill(s->pid, 0);
if (i != 0 && errno == ESRCH) {
/* Must be a crash */
s->state = SESSION_CRASHED;
} else {
/* Could be running */
/* Could check date here to see if reasonable */
s->state = SESSION_UNKNOWN;
}
}
session_list = g_list_append(session_list, s);
}
closedir(d);
/* Search for leftover tempfiles */
for (i=0; ; i++) {
c = get_temp_directory(i);
if (c == NULL) break;
d = opendir(c);
if (d == NULL) {
console_perror(c);
continue;
}
while (1) {
de = readdir(d);
if (de == NULL) break;
j = sscanf(de->d_name,"mhwaveedit-temp-%d-%d-%d",&k,&l,&m);
if (j < 2)
continue;
/* Try to stat the file */
p = g_strdup_printf("%s/%s",c,de->d_name);
n = stat(p,&st);
if (n < 0) {
console_perror(p);
g_free(p);
continue;
} else if (!S_ISREG(st.st_mode)) {
q = g_strdup_printf(_("%s: Wrong file type"),p);
console_message(q);
g_free(p);
g_free(q);
continue;
}
/* Try to add to known session */
for (list=session_list; list!=NULL; list=list->next) {
s = (struct session *)list->data;
if ((j>2 && m==s->id) ||
(j<3 && s->pid == k && s->state==SESSION_OLD))
break;
}
/* Create new session */
if (list == NULL) {
s = g_malloc0(sizeof(*s));
s->pid = k;
if (j>2) {
s->state = SESSION_LEFTOVER;
s->id = m;
} else {
s->state = SESSION_OLD;
s->id = ++highest_id;
}
session_list = g_list_append(session_list, s);
}
/* Add file to data file list */
s->datafiles = g_list_append(s->datafiles, p);
/* Update session info */
s->size += st.st_size;
if (s->date < st.st_mtime) s->date = st.st_mtime;
}
closedir(d);
}
current_id = highest_id+1;
/* Remove empty sessions unless the session was suspended by user */
for (list=session_list; list!=NULL; list=list->next) {
s = (struct session *) list->data;
if (s->state != SESSION_SUSPENDED && s->datafiles == NULL) {
if (s->logfile != NULL)
xunlink(s->logfile);
list2 = list->prev;
session_list = g_list_remove(session_list, s);
if (session_list == NULL) break;
if (list2 != NULL) list = list2; else list = session_list;
}
}
/* Sort list by date */
session_list = g_list_sort(session_list, session_compare_func);
/* Debug */
/*
puts("Sessions:");
for (list=session_list; list!=NULL; list=list->next) {
s = (struct session *)list->data;
printf("#%d: state=%d, size=%d\n",s->id,s->state,(int)s->size);
for (list2=s->datafiles; list2!=NULL; list2=list2->next) {
printf(" %s\n",(char *)list2->data);
}
} */
current_filename = g_strdup_printf("%s/mhwaveedit-session-%d-%d-r",
session_dir,(int)getpid(),current_id);
b = report_write_errors;
report_write_errors = FALSE;
current_file = e_fopen(current_filename,EFILE_WRITE);
if (current_file == NULL)
console_perror(_("Could not create session file"));
report_write_errors = b;
}
gint session_get_id(void)
{
return current_id;
}
static void session_resume(struct session *s)
{
GList *l;
gchar *fn;
Mainwindow *w;
g_assert(s->state == SESSION_CRASHED || s->state == SESSION_LEFTOVER ||
s->state == SESSION_OLD);
for (l=s->datafiles; l!=NULL; l=l->next) {
fn = (gchar *) l->data;
w = MAINWINDOW(mainwindow_new_with_file(fn,FALSE));
gtk_widget_show(GTK_WIDGET(w));
if (w->doc == NULL) continue;
/* This will rename the file to a regular tempfile name for this
* session and change the reference from a regular to a tempfile.
* If the file was converted into a new tempfile by the loader so
* that the original file is no longer being used, it will just be
* removed */
datasource_backup_unlink(fn);
/* Sets the title name to "untitled X" and makes the document appear
* "changed" so the "save?" dialog pops up when closing etc. */
document_forget_filename(w->doc);
}
/* Remove the session. */
g_list_foreach(s->datafiles, (GFunc)g_free, NULL);
g_list_free(s->datafiles);
g_free(s->logfile);
session_list = g_list_remove(session_list, s);
/* Popup notification */
if (!inifile_get_gboolean("crashMsgShown",FALSE)) {
user_message(_("The files that belonged to the crashed session have been "
"recovered. Any files that are not saved will be "
"removed permanently.\n\n(This notice is only shown once)"),
UM_OK);
inifile_set_gboolean("crashMsgShown",TRUE);
}
}
void session_quit(void)
{
if (current_file != NULL)
e_fclose_remove(current_file);
}
static GtkWidget *resume_button;
static gboolean destroy_flag = FALSE, resume_click_flag = FALSE;
static gint resume_index;
static void session_dialog_destroy(void)
{
destroy_flag = TRUE;
}
static void session_dialog_select_child(GtkList *list, GtkWidget *widget,
gpointer user_data)
{
resume_index = gtk_list_child_position(list,widget);
gtk_widget_set_sensitive(resume_button,TRUE);
}
static void session_dialog_exit(void)
{
quitflag = TRUE;
}
static void session_dialog_resume_click(void)
{
resume_click_flag = TRUE;
}
gboolean session_dialog(void)
{
GtkWidget *a,*b,*c,*d;
GList *l = NULL,*m;
int *im,i,j;
struct session *s;
gchar *ch,*p;
/* Running and unknown are never shown. Old is also called crash */
gchar *state_names[] = { NULL, _("Suspended"), _("Crash"),
_("Left files"), _("Crash"), NULL };
if (session_list == NULL) return FALSE;
im = g_malloc(g_list_length(session_list) * sizeof(int));
for (m=session_list,i=0,j=0; m!=NULL; m=m->next,j++) {
s = (struct session *) m->data;
if (s->state == SESSION_RUNNING || s->state == SESSION_UNKNOWN)
continue;
ch = g_strdup_printf(_("%s on %s(%d files, %ld bytes)"),
state_names[s->state],ctime(&(s->date)),
g_list_length(s->datafiles),(long)s->size);
/* Replace the newline returned by ctime */
p = strchr(ch,'\n');
if (p) *p=' ';
l = g_list_append(l,gtk_list_item_new_with_label(ch));
g_free(ch);
im[i++] = j;
}
if (l == NULL) {
g_free(im);
return FALSE;
}
a = gtk_window_new(GTK_WINDOW_DIALOG);
gtk_window_set_title(GTK_WINDOW(a),_("Sessions"));
gtk_window_set_modal(GTK_WINDOW(a),TRUE);
gtk_window_set_default_size(GTK_WINDOW(a),400,200);
gtk_container_set_border_width(GTK_CONTAINER(a),5);
gtk_signal_connect(GTK_OBJECT(a),"destroy",
GTK_SIGNAL_FUNC(session_dialog_destroy),NULL);
b = gtk_vbox_new(FALSE,5);
gtk_container_add(GTK_CONTAINER(a),b);
c = gtk_label_new(_("Earlier sessions were found. Choose one to resume or"
" start a new session."));
gtk_label_set_line_wrap(GTK_LABEL(c),TRUE);
gtk_box_pack_start(GTK_BOX(b),c,FALSE,FALSE,0);
c = gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(c),GTK_POLICY_NEVER,
GTK_POLICY_ALWAYS);
gtk_box_pack_start(GTK_BOX(b),c,TRUE,TRUE,0);
d = gtk_list_new();
gtk_list_insert_items(GTK_LIST(d),l,0);
gtk_signal_connect(GTK_OBJECT(d),"select_child",
GTK_SIGNAL_FUNC(session_dialog_select_child),NULL);
gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(c),d);
c = gtk_hbutton_box_new();
gtk_box_pack_end(GTK_BOX(b),c,FALSE,TRUE,0);
d = gtk_button_new_with_label(_("Resume selected"));
resume_button = d;
gtk_widget_set_sensitive(d,FALSE);
gtk_signal_connect(GTK_OBJECT(d),"clicked",
GTK_SIGNAL_FUNC(session_dialog_resume_click),NULL);
gtk_signal_connect_object(GTK_OBJECT(d),"clicked",
GTK_SIGNAL_FUNC(gtk_widget_destroy),
(GtkObject *)a);
gtk_container_add(GTK_CONTAINER(c),d);
d = gtk_button_new_with_label(_("Start new session"));
gtk_signal_connect_object(GTK_OBJECT(d),"clicked",
GTK_SIGNAL_FUNC(gtk_widget_destroy),
(GtkObject *)a);
gtk_container_add(GTK_CONTAINER(c),d);
d = gtk_button_new_with_label(_("Exit"));
gtk_signal_connect(GTK_OBJECT(d),"clicked",
GTK_SIGNAL_FUNC(session_dialog_exit),NULL);
gtk_signal_connect_object(GTK_OBJECT(d),"clicked",
GTK_SIGNAL_FUNC(gtk_widget_destroy),
(GtkObject *)a);
gtk_container_add(GTK_CONTAINER(c),d);
c = gtk_hseparator_new();
gtk_box_pack_end(GTK_BOX(b),c,FALSE,TRUE,0);
destroy_flag = resume_click_flag = FALSE;
gtk_widget_show_all(a);
while (!destroy_flag) mainloop(TRUE);
if (!resume_click_flag) return FALSE;
session_resume((struct session *)
g_list_nth(session_list,im[resume_index])->data);
return TRUE;
}
|