]> git.lyx.org Git - lyx.git/blob - src/lyx_sendfax_main.C
Dekel's patch -- I didn't fix the xforms-0.88 keysyms stuff so it still doesn't finis...
[lyx.git] / src / lyx_sendfax_main.C
1
2 #include <config.h>
3
4 #include <cstdlib>
5 #include FORMS_H_LOCATION
6 #include "debug.h"
7 #include "lyx_sendfax.h"
8 #include "lyx_main.h"
9 #include "lyxrc.h"
10 #include "support/filetools.h"
11 #include "lyx_gui_misc.h" // CancelCloseBoxCB
12 #include "support/syscall.h"
13 #include "gettext.h"
14
15 using std::endl;
16
17 /* Prototypes */
18
19 bool sendfax(string const & fname, string const & sendcmd);
20 bool button_send(string const & fname, string const & sendcmd = string());
21
22 static 
23 void show_logfile(string const & logfile, bool show_if_empty);
24
25 const int LEN_PHONE = 20;
26 const int LEN_NAME = 25;
27
28 /* Global Variables */
29
30 string phone_book;
31 string global_sendcmd;
32 string filename;
33
34 FD_xsendfax * fd_xsendfax;
35 FD_phonebook * fd_phonebook;
36 FD_logfile * fd_logfile;
37
38
39 bool send_fax(string const & fname, string const & sendcmd)
40 {
41     // I put FAXCMD here to see if I can get rid of some strange errors. (Lgb)
42     string FAXCMD = "sendfax -n -h '$$Host' -c '$$Comment' -x '$$Enterprise' -d '$$Name'@'$$Phone' '$$FName'";
43     string title = _("Fax File: ");
44     string path;
45     
46     if (fname.empty())
47         return false;
48     path = OnlyPath(fname);
49     if (path.empty() || path == "./")
50         filename = GetCWD() + "/" + fname;
51     else
52         filename = fname;
53
54     if (sendcmd.empty())
55             global_sendcmd = FAXCMD;
56     else
57             global_sendcmd = sendcmd;
58     
59     fd_xsendfax = create_form_xsendfax();
60     fd_phonebook = create_form_phonebook();
61     fd_logfile = create_form_logfile();
62
63     // Make sure the close box doesn't kill LyX when being pressed. (RvdK)
64     fl_set_form_atclose(fd_xsendfax->xsendfax, CancelCloseBoxCB, 0);
65     fl_set_form_atclose(fd_phonebook->phonebook, CancelCloseBoxCB, 0);
66     fl_set_form_atclose(fd_logfile->logfile, CancelCloseBoxCB, 0);
67
68     /* init Phone-Book */
69     string phone_book_name;
70     if (lyxrc.phone_book.empty()) {
71         phone_book_name = "phonebook";
72     } else
73         phone_book_name = lyxrc.phone_book;
74     phone_book= FileSearch(user_lyxdir, phone_book_name);
75     if (phone_book.empty()) 
76         phone_book = AddName(user_lyxdir, phone_book_name);
77
78     fl_set_browser_fontsize(fd_phonebook->browser, FL_NORMAL_SIZE);
79     fl_set_browser_fontstyle(fd_phonebook->browser, FL_FIXED_STYLE);
80     fl_load_browser(fd_phonebook->browser, phone_book.c_str());
81
82     title += OnlyFilename(fname);
83
84     /* show the first form */
85     fl_show_form(fd_xsendfax->xsendfax,
86                  FL_PLACE_MOUSE, FL_FULLBORDER, title.c_str());
87 /*
88     while(true) {
89         obj = fl_do_forms();
90         if (obj == fd_xsendfax->Button_Cancel)
91             break;
92         else if (obj == fd_xsendfax->Button_Send) {
93             if (button_send(fname, global_sendcmd))
94                 break;
95         } else if (obj == fd_xsendfax->Button_Apply) {
96             button_send(fname, global_sendcmd);
97         } else if (obj == fd_xsendfax->Button_SPhone) {
98             cb_select_phoneno(0, 0);
99         }
100     }
101     fl_hide_form(fd_xsendfax->xsendfax);
102 */
103     return true;
104 }
105
106
107 bool button_send(string const & fname, string const & sendcmd)
108 {
109     string name = fl_get_input(fd_xsendfax->Input_Name);
110     string phone = fl_get_input(fd_xsendfax->Input_Phone);
111     string enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
112     string comment = fl_get_input(fd_xsendfax->Input_Comment);
113     string host = GetEnv("FAX_SERVER");
114
115     if (phone.empty())
116         return false;
117     string logfile = TmpFileName(OnlyPath(fname), "FAX");
118     string cmd = sendcmd + " >";
119     cmd += logfile + " 2>";
120     cmd += logfile;
121     cmd = subst(cmd, "$$Host", host);
122     cmd = subst(cmd, "$$Comment", comment);
123     cmd = subst(cmd, "$$Enterprise", enterprise);
124     cmd = subst(cmd, "$$Name", name);
125     cmd = subst(cmd, "$$Phone", phone);
126     cmd = subst(cmd, "$$FName", fname);
127     lyxerr << "CMD: " << cmd << endl;
128     Systemcalls one(Systemcalls::System, cmd);
129     show_logfile(logfile, false);
130     remove(logfile.c_str());
131     return true;
132 }
133
134
135 void cb_hide_form(FL_OBJECT * ob, long)
136 {
137     fl_hide_form(ob->form);
138 }
139
140
141 void cb_select_phoneno(FL_OBJECT *, long)
142 {
143     int n = fl_get_browser_maxline(fd_phonebook->browser);
144
145     fl_hide_form(fd_phonebook->phonebook);
146     char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
147     if (!n || strstr(line, _("Empty Phonebook"))) {
148         fl_clear_browser(fd_phonebook->browser);
149         return;
150     }
151     int i = fl_get_browser(fd_phonebook->browser);
152     line = fl_get_browser_line(fd_phonebook->browser, i);
153     if (!line)
154         return;
155     char * buf = new char [strlen(line)+1];
156
157     strcpy(buf, line);
158     for(i = LEN_PHONE; (i > 0) && (buf[i] == ' '); --i)
159         ;
160     buf[i + 1] = 0;
161     for(i = LEN_PHONE + LEN_NAME + 1; (i > LEN_PHONE) && (buf[i] == ' '); --i)
162         ;
163     buf[i + 1] = 0;
164     fl_set_input(fd_xsendfax->Input_Phone, buf);
165     fl_set_input(fd_xsendfax->Input_Name, buf+LEN_PHONE+1);
166     fl_set_input(fd_xsendfax->Input_Enterprise, buf+LEN_PHONE+LEN_NAME+2);
167     delete [] buf;
168 }
169
170
171 void cb_add_phoneno(FL_OBJECT *, long )
172 {
173     char const * phone = fl_get_input(fd_xsendfax->Input_Phone);
174
175     if (!strlen(phone))
176         return;
177
178     char const * name = fl_get_input(fd_xsendfax->Input_Name);
179     char const * enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
180
181     char * buf = new char [50 + strlen(enterprise)];
182
183     sprintf(buf, "%-*.*s %-*.*s %s",
184             LEN_PHONE, LEN_PHONE, phone,
185             LEN_NAME, LEN_NAME, name,
186             enterprise);
187     int n = fl_get_browser_maxline(fd_phonebook->browser);
188     if (n) {
189         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
190         if (strstr(line, _("Empty Phonebook"))) {
191             fl_clear_browser(fd_phonebook->browser);
192             n = 0;
193         }
194     }
195     int i = 1;
196     for(; i <= n; ++i) {
197         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
198         if (!strncmp(buf, line, 46))
199             break;
200     }
201     if (i > n) {
202         fl_addto_browser(fd_phonebook->browser, buf);
203         fl_set_object_label(fd_xsendfax->pb_save, _("Save (needed)"));
204     }
205     delete[] buf;
206 }
207
208
209 void cb_delete_phoneno(FL_OBJECT *, long )
210 {
211     char const * phone = fl_get_input(fd_xsendfax->Input_Phone);
212
213     if (!strlen(phone))
214         return;
215
216     char const * name = fl_get_input(fd_xsendfax->Input_Name);
217     char const * enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
218
219     char * buf = new char [50+strlen(enterprise)];
220
221     sprintf(buf, "%-*.*s %-*.*s %s",
222             LEN_PHONE, LEN_PHONE, phone,
223             LEN_NAME, LEN_NAME, name,
224             enterprise);
225     int n = fl_get_browser_maxline(fd_phonebook->browser);
226     if (n) {
227         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
228         if (strstr(line, _("Empty Phonebook"))) {
229             fl_clear_browser(fd_phonebook->browser);
230             n = 0;
231         }
232     }
233     int i = 1;
234     for(; i <= n; ++i) {
235         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
236         if (!strncmp(buf, line, 46))
237             break;
238     }
239     if (i <= n) {
240         fl_delete_browser_line(fd_phonebook->browser, i);
241         fl_set_object_label(fd_xsendfax->pb_save, _("Save (needed)"));
242     }
243     delete[] buf;
244 }
245
246
247 void cb_save_phoneno(FL_OBJECT *, long )
248 {
249     FILE * fp = fopen(phone_book.c_str(), "w");
250     if (!fp) {
251         WriteAlert(_("Error!"), _("Cannot open phone book: "), phone_book);
252         return;
253     }
254     int n = fl_get_browser_maxline(fd_phonebook->browser);
255     if (n) {
256         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
257         if (strstr(line, _("Empty Phonebook"))) {
258             fl_clear_browser(fd_phonebook->browser);
259             n = 0;
260         }
261     }
262     int i = 1;
263     for(; i <= n; ++i) {
264         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
265         fprintf(fp, "%s\n", line);
266     }
267     fclose(fp);
268     fl_set_object_label(fd_xsendfax->pb_save, _("Save"));
269     fl_redraw_form(fd_xsendfax->xsendfax);
270 }
271
272
273 static
274 void show_logfile(string const & logfile, bool show_if_empty)
275 {
276     if (logfile.empty())
277         return;
278     if (!fl_load_browser(fd_logfile->browser, logfile.c_str())) {
279         if (!show_if_empty)
280             return;
281         fl_add_browser_line(fd_logfile->browser,
282                             _("NO OR EMPTY LOGFILE!"));
283     }
284     if (fd_logfile->logfile->visible) {
285         fl_raise_form(fd_logfile->logfile);
286     } else {
287         fl_show_form(fd_logfile->logfile,
288                      FL_PLACE_MOUSE | FL_FREE_SIZE, FL_FULLBORDER,
289                      _("Message-Window"));
290     }
291 }
292
293
294 void FaxLogfileCloseCB(FL_OBJECT *, long)
295 {
296     fl_hide_form(fd_logfile->logfile);
297 }
298
299
300 void FaxCancelCB(FL_OBJECT *, long)
301 {
302     fl_hide_form(fd_xsendfax->xsendfax);
303 }
304
305
306 void FaxApplyCB(FL_OBJECT *, long)
307 {
308     button_send(filename, global_sendcmd);
309 }
310
311
312 void FaxSendCB(FL_OBJECT *, long)
313 {
314     if (button_send(filename, global_sendcmd))
315         FaxCancelCB(0, 0);
316 }
317
318
319 void FaxOpenPhonebookCB(FL_OBJECT *, long)
320 {
321     int n = fl_get_browser_maxline(fd_phonebook->browser);
322
323     if (!n)
324         fl_addto_browser(fd_phonebook->browser, _("@L@b@cEmpty Phonebook"));
325     fl_show_form(fd_phonebook->phonebook, FL_PLACE_MOUSE, FL_FULLBORDER,
326                  _("Phonebook"));
327 }