]> git.lyx.org Git - lyx.git/blob - src/lyx_sendfax_main.C
prepare for 1.1.6pre2
[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 = lyx::getcwd() /*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_FREE_SIZE, FL_TRANSIENT,
87                  title.c_str());
88 /*
89     while(true) {
90         obj = fl_do_forms();
91         if (obj == fd_xsendfax->Button_Cancel)
92             break;
93         else if (obj == fd_xsendfax->Button_Send) {
94             if (button_send(fname, global_sendcmd))
95                 break;
96         } else if (obj == fd_xsendfax->Button_Apply) {
97             button_send(fname, global_sendcmd);
98         } else if (obj == fd_xsendfax->Button_SPhone) {
99             cb_select_phoneno(0, 0);
100         }
101     }
102     fl_hide_form(fd_xsendfax->xsendfax);
103 */
104     return true;
105 }
106
107
108 bool button_send(string const & fname, string const & sendcmd)
109 {
110     string name = fl_get_input(fd_xsendfax->Input_Name);
111     string phone = fl_get_input(fd_xsendfax->Input_Phone);
112     string enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
113     string comment = fl_get_input(fd_xsendfax->Input_Comment);
114     string host = GetEnv("FAX_SERVER");
115
116     if (phone.empty())
117         return false;
118     string logfile = lyx::tempName(OnlyPath(fname), "FAX"); //TmpFileName(OnlyPath(fname), "FAX");
119     string cmd = sendcmd + " >";
120     cmd += logfile + " 2>";
121     cmd += logfile;
122     cmd = subst(cmd, "$$Host", host);
123     cmd = subst(cmd, "$$Comment", comment);
124     cmd = subst(cmd, "$$Enterprise", enterprise);
125     cmd = subst(cmd, "$$Name", name);
126     cmd = subst(cmd, "$$Phone", phone);
127     cmd = subst(cmd, "$$FName", fname);
128     lyxerr << "CMD: " << cmd << endl;
129     Systemcalls one(Systemcalls::System, cmd);
130     show_logfile(logfile, false);
131     lyx::unlink(logfile);
132     return true;
133 }
134
135
136 void cb_hide_form(FL_OBJECT * ob, long)
137 {
138     fl_hide_form(ob->form);
139 }
140
141
142 void cb_select_phoneno(FL_OBJECT *, long)
143 {
144     int n = fl_get_browser_maxline(fd_phonebook->browser);
145
146     fl_hide_form(fd_phonebook->phonebook);
147     char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
148     if (!n || strstr(line, _("Empty Phonebook"))) {
149         fl_clear_browser(fd_phonebook->browser);
150         return;
151     }
152     int i = fl_get_browser(fd_phonebook->browser);
153     line = fl_get_browser_line(fd_phonebook->browser, i);
154     if (!line)
155         return;
156     char * buf = new char [strlen(line)+1];
157
158     strcpy(buf, line);
159     for(i = LEN_PHONE; (i > 0) && (buf[i] == ' '); --i)
160         ;
161     buf[i + 1] = 0;
162     for(i = LEN_PHONE + LEN_NAME + 1; (i > LEN_PHONE) && (buf[i] == ' '); --i)
163         ;
164     buf[i + 1] = 0;
165     fl_set_input(fd_xsendfax->Input_Phone, buf);
166     fl_set_input(fd_xsendfax->Input_Name, buf+LEN_PHONE+1);
167     fl_set_input(fd_xsendfax->Input_Enterprise, buf+LEN_PHONE+LEN_NAME+2);
168     delete [] buf;
169 }
170
171
172 void cb_add_phoneno(FL_OBJECT *, long )
173 {
174     char const * phone = fl_get_input(fd_xsendfax->Input_Phone);
175
176     if (!strlen(phone))
177         return;
178
179     char const * name = fl_get_input(fd_xsendfax->Input_Name);
180     char const * enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
181
182     char * buf = new char [50 + strlen(enterprise)];
183
184     sprintf(buf, "%-*.*s %-*.*s %s",
185             LEN_PHONE, LEN_PHONE, phone,
186             LEN_NAME, LEN_NAME, name,
187             enterprise);
188     int n = fl_get_browser_maxline(fd_phonebook->browser);
189     if (n) {
190         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
191         if (strstr(line, _("Empty Phonebook"))) {
192             fl_clear_browser(fd_phonebook->browser);
193             n = 0;
194         }
195     }
196     int i = 1;
197     for(; i <= n; ++i) {
198         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
199         if (!strncmp(buf, line, 46))
200             break;
201     }
202     if (i > n) {
203         fl_addto_browser(fd_phonebook->browser, buf);
204         fl_set_object_label(fd_xsendfax->pb_save, _("Save (needed)"));
205     }
206     delete[] buf;
207 }
208
209
210 void cb_delete_phoneno(FL_OBJECT *, long )
211 {
212     char const * phone = fl_get_input(fd_xsendfax->Input_Phone);
213
214     if (!strlen(phone))
215         return;
216
217     char const * name = fl_get_input(fd_xsendfax->Input_Name);
218     char const * enterprise = fl_get_input(fd_xsendfax->Input_Enterprise);
219
220     char * buf = new char [50+strlen(enterprise)];
221
222     sprintf(buf, "%-*.*s %-*.*s %s",
223             LEN_PHONE, LEN_PHONE, phone,
224             LEN_NAME, LEN_NAME, name,
225             enterprise);
226     int n = fl_get_browser_maxline(fd_phonebook->browser);
227     if (n) {
228         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
229         if (strstr(line, _("Empty Phonebook"))) {
230             fl_clear_browser(fd_phonebook->browser);
231             n = 0;
232         }
233     }
234     int i = 1;
235     for(; i <= n; ++i) {
236         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
237         if (!strncmp(buf, line, 46))
238             break;
239     }
240     if (i <= n) {
241         fl_delete_browser_line(fd_phonebook->browser, i);
242         fl_set_object_label(fd_xsendfax->pb_save, _("Save (needed)"));
243     }
244     delete[] buf;
245 }
246
247
248 void cb_save_phoneno(FL_OBJECT *, long )
249 {
250     FILE * fp = ::fopen(phone_book.c_str(), "w");
251     if (!fp) {
252         WriteAlert(_("Error!"), _("Cannot open phone book: "), phone_book);
253         return;
254     }
255     int n = fl_get_browser_maxline(fd_phonebook->browser);
256     if (n) {
257         char const * line = fl_get_browser_line(fd_phonebook->browser, 1);
258         if (strstr(line, _("Empty Phonebook"))) {
259             fl_clear_browser(fd_phonebook->browser);
260             n = 0;
261         }
262     }
263     int i = 1;
264     for(; i <= n; ++i) {
265         char const * line = fl_get_browser_line(fd_phonebook->browser, i);
266         fprintf(fp, "%s\n", line);
267     }
268     fclose(fp);
269     fl_set_object_label(fd_xsendfax->pb_save, _("Save"));
270     fl_redraw_form(fd_xsendfax->xsendfax);
271 }
272
273
274 static
275 void show_logfile(string const & logfile, bool show_if_empty)
276 {
277     if (logfile.empty())
278         return;
279     if (!fl_load_browser(fd_logfile->browser, logfile.c_str())) {
280         if (!show_if_empty)
281             return;
282         fl_add_browser_line(fd_logfile->browser,
283                             _("NO OR EMPTY LOGFILE!"));
284     }
285     if (fd_logfile->logfile->visible) {
286         fl_raise_form(fd_logfile->logfile);
287     } else {
288         fl_show_form(fd_logfile->logfile,
289                      FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
290                      _("Message-Window"));
291     }
292 }
293
294
295 void FaxLogfileCloseCB(FL_OBJECT *, long)
296 {
297     fl_hide_form(fd_logfile->logfile);
298 }
299
300
301 void FaxCancelCB(FL_OBJECT *, long)
302 {
303     fl_hide_form(fd_xsendfax->xsendfax);
304 }
305
306
307 void FaxApplyCB(FL_OBJECT *, long)
308 {
309     button_send(filename, global_sendcmd);
310 }
311
312
313 void FaxSendCB(FL_OBJECT *, long)
314 {
315     if (button_send(filename, global_sendcmd))
316         FaxCancelCB(0, 0);
317 }
318
319
320 void FaxOpenPhonebookCB(FL_OBJECT *, long)
321 {
322     int n = fl_get_browser_maxline(fd_phonebook->browser);
323
324     if (!n)
325         fl_addto_browser(fd_phonebook->browser, _("@L@b@cEmpty Phonebook"));
326     fl_show_form(fd_phonebook->phonebook,
327                  FL_PLACE_MOUSE | FL_FREE_SIZE, FL_TRANSIENT,
328                  _("Phonebook"));
329 }