File: status.c

package info (click to toggle)
melon 1.4-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 956 kB
  • ctags: 158
  • sloc: ansic: 2,427; makefile: 91; sh: 58
file content (186 lines) | stat: -rw-r--r-- 4,637 bytes parent folder | download
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
/* status.c -- settings for saving  mailbox status
 *
 * Copyright(C) 2001-2002 Elisa Manara <e@entropika.net>
 * This code is released under the GPL License version 2 */

#include <gtk/gtk.h>
#include <stdio.h>
#include <stdlib.h>
#include "melon.h"

static void update_save_status(GtkWidget *widget, gpointer user_data);
static void status_file_update (void);
static void set_status_file(char *status_file_path);

char *set_melon_dir(void);

gint set_save_status (GtkWidget *widget, gpointer data)
{
	GtkWidget *window;
	GtkWidget *vbox, *hbox1, *hbox2;
	GtkWidget *button, *label, *frame;

	/* window */
	window = gtk_window_new( GTK_WINDOW_DIALOG);
	gtk_window_set_position(GTK_WINDOW (window), GTK_WIN_POS_MOUSE);
	gtk_widget_set_usize( GTK_WIDGET (window), 300, 180);
	gtk_window_set_title(GTK_WINDOW (window), "Save Mailbox Status");
	gtk_widget_show(window);

	/* vbox */
	vbox = gtk_vbox_new( FALSE, 0 );
	gtk_container_add( GTK_CONTAINER( window ), vbox );
	gtk_widget_show( vbox );

	/* frame */
	frame = gtk_frame_new (NULL);
	/* label */
	label = gtk_label_new ("\nMelon can save the status of your mailboxes before quitting.\n" \
			       "Next time you'll launch it, Melon will be able to "\
			       "notify if new mails was received since it was "\
			       "off.\n\nShould Melon remember the mailboxes status?\n\n\n");

	gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_FILL);
	gtk_label_set_line_wrap (GTK_LABEL (label), TRUE);
	gtk_container_add (GTK_CONTAINER (frame), label);
	gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 5);

	gtk_widget_show(label);
	gtk_widget_show(frame);

	/* hbox1 */
	hbox1 = gtk_hbox_new( FALSE, 0 );
	//gtk_container_add( GTK_CONTAINER( vbox ), hbox1);
	gtk_box_pack_start (GTK_BOX ( vbox ), hbox1, TRUE, TRUE, 0);
	gtk_widget_show( hbox1 );

	/* hbox2 */
	hbox2 = gtk_hbox_new( FALSE, 0 );
	gtk_container_add( GTK_CONTAINER( vbox ), hbox2);
	gtk_widget_show( hbox2 );

	/* YES button */
	button = gtk_button_new_with_label ("Yes");
	/*
	gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
		GTK_SIGNAL_FUNC(update_save_status), "yes");
	*/
	gtk_signal_connect( GTK_OBJECT( button ), "clicked",
		GTK_SIGNAL_FUNC( update_save_status ), "yes");

	gtk_box_pack_start (GTK_BOX (hbox2), button, TRUE, TRUE, 0);
	gtk_widget_show (button);

	/* NO button */
	button = gtk_button_new_with_label ("No");
	/*
	gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
		GTK_SIGNAL_FUNC(update_save_status), "no");
	*/
	gtk_signal_connect( GTK_OBJECT( button ), "clicked",
		GTK_SIGNAL_FUNC( update_save_status ), "no");

	gtk_box_pack_start (GTK_BOX (hbox2), button, TRUE, TRUE, 0);
	gtk_widget_show (button);

	/* CLOSE button */
	button = gtk_button_new_with_label ("Close");
	gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
		GTK_SIGNAL_FUNC(gtk_widget_destroy),
		GTK_OBJECT (window));
	gtk_box_pack_start (GTK_BOX (hbox2), button, TRUE, TRUE, 0);
	gtk_widget_show (button);

	return 1;

}

void update_save_status (GtkWidget *widget, gpointer user_data)
{
	if (strcmp(user_data, "yes") == 0) {
		cfg_data.save_status = 1;
		dialog_message (" Melon will save mailbox status ");
	} else if (strcmp(user_data, "no") == 0) {
		dialog_message (" Melon will NOT save mailbox status ");
		cfg_data.save_status = 0;
	}

	config_update();

	return;
}

void save_status (void)
{
	if(cfg_data.save_status == 1) {
		status_file_update();
	}

	return;
}

void status_file_update (void)
{
	FILE *fp;
	int i;
	char status_file[MAX_PLEN];

	set_status_file(status_file);

	if((fp = fopen(status_file, "w")) == NULL) {
		char msg[1024];
		snprintf(msg, 1024, " Cannot open %s for writing ", status_file);
		dialog_message (msg);
		return;
	}

	for(i = 0; i < MAX_MBOX; i++)
	{
		if(cfg_data.mbox[i][0] == '\0')
			break;

		fprintf(fp,"%s %ld %ld\n", cfg_data.mbox[i], w_data.m_time[i], w_data.size[i]);
	}
	fclose(fp);
	return;
}

void set_status_file(char *status_file_path)
{
	char *melon_home_dir=set_melon_dir();

	snprintf(status_file_path, MAX_PLEN, "%s/%s", melon_home_dir, "melon.status");
	free(melon_home_dir);
}

int retrieve_mbox_status(char *mbox, int i) {

	FILE *fp;
	char buf[1024];
	char mbox_path[MAX_PLEN] = {'\0'};
	long old_time = 0;
	long old_size = 0;
	char status_file[MAX_PLEN];

	set_status_file(status_file);

	if((fp = fopen(status_file, "r")) == NULL)
		return 0;
	
	while(fgets(buf, 1024, fp) != NULL)
	{
		if(sscanf(buf, "%s %ld %ld\n", mbox_path, &old_time, &old_size) != 3)
			continue;
		if(strcmp(mbox_path, mbox) == 0) {
			w_data.m_time[i] = old_time;
			w_data.size[i] = old_size;
			return 1;
		}
	}
	fclose(fp);

	return 0;
}