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