1 /* pal
2  *
3  * Copyright (C) 2004, Scott Kuhl
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  */
20 
21 #include <stdio.h>
22 #include <ncurses.h>
23 
24 #include "main.h"
25 #include "output.h"
26 #include "event.h"
27 #include "rl.h"
28 #include "input.h"
29 #include "edit.h"
30 
pal_del_write_file(PalEvent * dead_event)31 void pal_del_write_file(PalEvent* dead_event)
32 {
33     FILE *file = NULL;
34     gchar* filename = g_strdup(dead_event->file_name);
35     FILE *out_file = NULL;
36     gchar *out_filename = NULL;
37     PalEvent* event_head = NULL;
38 
39     g_strstrip(filename);
40     out_filename = g_strconcat(filename, ".paltmp", NULL);
41 
42 
43     file = fopen(filename, "r");
44     if(file == NULL)
45     {
46 	pal_output_error(_("ERROR: Can't read file: %s\n"), filename);
47 	pal_output_error(_("       The event was NOT deleted."));
48 	return;
49     }
50 
51     out_file = fopen(out_filename, "w");
52     if(out_file == NULL)
53     {
54 	pal_output_error(_("ERROR: Can't write file: %s\n"), out_filename);
55 	pal_output_error(_("       The event was NOT deleted."));
56 	if(file != NULL)
57 	    fclose(file);
58 	return;
59     }
60 
61 
62 
63     pal_input_skip_comments(file, out_file);
64     event_head = pal_input_read_head(file, out_file, filename);
65 
66     while(1)
67     {
68 	PalEvent* pal_event = NULL;
69 
70 	pal_input_skip_comments(file, out_file);
71 
72 	pal_event = pal_input_read_event(file, out_file, filename, event_head, dead_event);
73 
74 	/* stop trying to delete dead_event if we just deleted it */
75 	if(dead_event != NULL && pal_event == dead_event)
76 	    dead_event = NULL;
77 	else if(pal_event == NULL && pal_input_eof(file))
78 	    break;
79     }
80 
81 
82     fclose(file);
83     fclose(out_file);
84 
85     if(rename(out_filename, filename) != 0)
86     {
87 	pal_output_error(_("ERROR: Can't rename %s to %s\n"), out_filename, filename);
88 	pal_output_error(_("       The event was NOT deleted."));
89 	return;
90     }
91 
92 
93     if(dead_event == NULL)
94     {
95 	pal_output_fg(BRIGHT, GREEN, ">>> ");
96 	g_print(_("Event removed from %s.\n"), filename);
97     }
98     else
99 	pal_output_error(_("ERROR: Couldn't find event to be deleted in %s"), filename);
100 
101     g_free(filename);
102 }
103 
pal_del_event(GDate * date,int eventnum)104 static void pal_del_event( GDate *date, int eventnum )
105 {
106     PalEvent* dead_event = NULL;
107     GDate* event_date = NULL;
108 
109     clear();
110     pal_output_fg(BRIGHT, GREEN, "* * * ");
111     pal_output_attr(BRIGHT, _("Delete an event"));
112     pal_output_fg(BRIGHT, GREEN, " * * *\n");
113 
114     pal_output_fg(BRIGHT, YELLOW, "> ");
115     pal_output_wrap(_("If you want to delete old events that won't occur again, you can use pal's -x option instead of deleting the events manually."),2,2);
116 
117     dead_event = pal_rl_get_event(&event_date, FALSE);
118 
119     g_print("\n");
120     pal_output_fg(BRIGHT, GREEN, "> ");
121     g_print(_("You have selected to delete the following event:\n"));
122     pal_output_event(dead_event, event_date, -1);
123 
124     if(pal_rl_get_y_n(_("Are you sure you want to delete this event? [y/n]: ")))
125        pal_del_write_file(dead_event);
126 
127     pal_main_reload();
128 
129 }
130