Add disk_applet executable to the ignore list
[rmail.git] / src / utils / test_list_x11.c
bloba99aaef1e827cf92054fa9c0975361e1c559d300
1 #define _BSD_SOURCE
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <unistd.h>
7 #include <dirent.h>
8 #include <errno.h>
9 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
14 #include <X11/Intrinsic.h>
15 #include <X11/StringDefs.h>
16 #include <X11/Shell.h>
18 #include <X11/Xaw/Form.h>
19 #include <X11/Xaw/Viewport.h>
20 #include <X11/Xaw/Command.h>
21 #include <X11/Xaw/AsciiText.h>
22 #include <X11/Xaw/Label.h>
23 #include <X11/Xaw/Box.h>
25 #include "md5_utils.h"
26 #include "string_utils.h"
27 #include "file_utils.h"
28 #include "rfc5322.h"
29 #include "rfc822.h"
30 #include "icons_utils.h"
32 #include "iv_widget.h"
34 #include "list.h"
36 struct applet {
37 XtAppContext ctx; /* Application context */
38 Widget shell; /* Application shell widget */
39 Widget form; /* Application top Form widget */
42 static struct mail_list *mlist;
44 void list_add_from_file(const char *filename)
46 FILE *fh;
47 struct mail_entry entry;
49 /* Check if is a regular file */
50 if (file_type(filename)) {
51 return;
54 fh = fopen(filename, "r");
55 if (!fh)
56 return;
58 strncpy(entry.hdr.filename, filename, LINE_LENGTH);
60 parse_mail_header(fh, &entry.hdr);
62 fclose(fh);
64 if (mlist) {
65 if (list_add_entry(mlist, &entry)) {
66 fprintf(stderr, "%s - No free slot for %s\n", __func__, filename);
67 } else {
68 /* fprintf(stderr, "%s - File %s added to list\n", __func__, filename); */
74 /*======================================================================
75 * X11 CODE
76 *======================================================================*/
78 /* The callback function for the quit button. */
79 void quit_callback(Widget w,
80 __attribute__((unused)) XtPointer client_data,
81 __attribute__((unused)) XtPointer call_data)
83 list_free(mlist);
85 XtDestroyApplicationContext(XtWidgetToApplicationContext(w));
87 exit(0);
90 static void fix_scrollbar_translations(Widget w, String name)
92 Widget sb = NULL; /* scrollbar widget */
94 /* New scrollbar translation */
95 String trans = "#override "
96 "<Btn1Down>: StartScroll(Forward)\n" /* Left button */
97 "<Btn2Down>: \n" /* Middle button */
98 "<Btn3Down>: StartScroll(Backward)\n" /* Right button */
99 "<Btn4Down>: StartScroll(Backward)\n" /* Wheel down */
100 "<Btn5Down>: StartScroll(Forward)\n" /* Wheel up */
101 "<Btn1Motion>: MoveThumb() NotifyThumb()\n"
102 "<BtnUp>: NotifyScroll(Proportional) EndScroll()";
104 if (!name)
105 return;
107 sb = XtNameToWidget(w, name);
108 if (sb)
109 XtOverrideTranslations(sb, XtParseTranslationTable(trans));
110 else
111 fprintf(stderr, "%s: WARNING cannon set %s translation!\n", __func__, name);
115 /* destroy_popup: Destroys the popup box widget.
116 * Arguments:
117 * w - *** UNUSED ***.
118 * box - the box widget. This widget is a direct child of the popup shell to destroy.
119 * p - *** UNUSED **.
121 static void destroy_popup(__attribute__((unused)) Widget w,
122 XtPointer box,
123 __attribute__((unused)) XtPointer p)
125 Widget popup;
127 popup = XtParent((Widget) box);
129 XtDestroyWidget(popup);
132 static void create_popup(Widget w, XtPointer client_data,
133 __attribute__((unused)) XtPointer call_data)
136 Widget popup;
137 Widget box;
138 Widget address;
139 Widget subj;
140 Widget date;
141 Widget text;
142 Widget ok_btn;
144 struct mail_entry *me = (struct mail_entry *) client_data;
146 popup = XtVaCreatePopupShell("PopupText", transientShellWidgetClass, w,
147 XtNborderWidth, 0,
148 XtNx, 100,
149 XtNy, 100,
150 /*XtNwidth, 400, */
151 NULL);
153 /* PopupBox contains Label and Slider */
154 box = XtVaCreateManagedWidget("PopupForm", formWidgetClass, popup,
155 XtNborderWidth, 0,
156 NULL);
158 address = XtVaCreateManagedWidget("Address", labelWidgetClass, box,
159 XtNborderWidth, 0,
160 XtNwidth, 400,
161 XtNlabel, me->hdr.from,
162 XtNjustify, XtJustifyLeft,
163 NULL);
165 date = XtVaCreateManagedWidget("date", labelWidgetClass, box,
166 XtNfromHoriz, address,
167 XtNborderWidth, 0,
168 XtNwidth, 400,
169 XtNlabel, rfc822_mkdt(me->hdr.date),
170 XtNjustify, XtJustifyLeft,
171 NULL);
173 subj = XtVaCreateManagedWidget("subj", labelWidgetClass, box,
174 XtNfromVert, date,
175 XtNborderWidth, 0,
176 XtNwidth, 400,
177 XtNlabel, me->hdr.subject,
178 XtNjustify, XtJustifyLeft,
179 NULL);
181 text = XtVaCreateManagedWidget("Text", asciiTextWidgetClass, box,
182 XtNfromVert, subj,
183 XtNresizable, True,
184 XtNtype, XawAsciiFile,
185 XtNstring, me->hdr.filename,
186 XtNwidth, 800,
187 XtNheight, 500,
188 XtNdisplayCaret, False,
189 XtNscrollHorizontal, XawtextScrollWhenNeeded,
190 XtNscrollVertical, XawtextScrollAlways,
191 XtNeditType, XawtextRead,
192 XtNtop, XtChainTop,
193 NULL);
195 ok_btn = XtVaCreateManagedWidget("Ok", commandWidgetClass, box,
196 XtNfromVert, text,
197 XtNleft, XtChainLeft,
198 XtNright, XtChainLeft,
199 XtNtop, XtChainBottom,
200 XtNbottom, XtChainBottom,
201 XtNresizable, False,
202 NULL);
204 XtAddCallback(ok_btn, XtNcallback, destroy_popup, (XtPointer) box);
206 /* Popup widget */
207 XtPopup(popup, XtGrabNone);
208 #if 0
209 fprintf(stderr, "%s box=%p popup=%p\n", __func__, (void *) box, (void *) popup);
210 #endif
213 static int create_applet(struct applet *app, int argc, char **argv)
215 XtSetLanguageProc(NULL, (XtLanguageProc) NULL, NULL);
217 /* Open Xt application shell widget */
218 app->shell = XtVaOpenApplication(&app->ctx, "TestListX11", NULL, 0,
219 &argc, argv, NULL,
220 applicationShellWidgetClass,
221 NULL);
223 /* Create top form widget */
224 app->form = XtVaCreateManagedWidget("AppForm", formWidgetClass,
225 app->shell, NULL);
226 return 0;
229 /* The callback function for the quit button. */
230 void item_callback(Widget w, XtPointer item_string, XtPointer call_data)
232 create_popup(w, item_string, call_data);
235 static int list_add_item(Widget parent, const struct mail_entry *item)
237 Display *d;
238 Visual *visual;
239 int screen;
241 /* Depth of the root window */
242 int depth;
244 char *icon = NULL;
245 XImage *pix = NULL;
247 Widget box_v;
248 Widget box_h;
249 Widget addr;
250 Widget subj;
251 Widget iv;
253 d = XtDisplay(parent);
255 screen = DefaultScreen(d);
256 depth = DisplayPlanes(d, screen);
257 visual = XDefaultVisual(d, screen);
259 /* Allocated icon pathname */
260 icon = icon_get_path((char *) item->hdr.addr_md5);
262 /* Create XImage from PNG file */
263 pix = create_ximage_from_PNG(d, visual, depth, icon, NULL);
265 /* Free allocated icon pathname with 'icon_get_path()' */
266 icon_free_path(icon);
268 box_h = XtVaCreateManagedWidget("box_h", boxWidgetClass, parent,
269 XtNorientation, XtorientHorizontal,
270 NULL);
272 iv = XtVaCreateManagedWidget("IV", imageViewWidgetClass, box_h,
273 XtNximage, pix,
274 XtNimageType, 0,
275 NULL);
277 box_v = XtVaCreateManagedWidget("box_v", boxWidgetClass, box_h,
278 XtNborderWidth, 0,
279 XtNorientation, XtorientVertical,
280 NULL);
282 addr = XtVaCreateManagedWidget("from", labelWidgetClass, box_v,
283 XtNborderWidth, 0,
284 XtNwidth, 400,
285 XtNlabel, item->hdr.from,
286 XtNjustify, XtJustifyLeft,
287 NULL);
289 subj = XtVaCreateManagedWidget("subj", labelWidgetClass, box_v,
290 XtNborderWidth, 0,
291 XtNwidth, 400,
292 XtNlabel, item->hdr.subject,
293 XtNjustify, XtJustifyLeft,
294 NULL);
296 /* Set handler for mouse button press on widgets
297 * Pass the file to the callbacks
298 * Button1Mask
299 * Button2Mask
300 * Button3Mask
301 * Button4Mask
302 * Button5Mask
303 * All the above ButtonPressMask
305 #if 0
306 XtAddEventHandler(addr, Button1Mask, False, (XtEventHandler) item_callback, (XtPointer) item);
307 XtAddEventHandler(subj, Button1Mask, False, (XtEventHandler) item_callback, (XtPointer) item);
308 XtAddEventHandler(iv, Button1Mask, False, (XtEventHandler) item_callback, (XtPointer) item);
309 #else
310 XtAddEventHandler(addr, ButtonPressMask, False, (XtEventHandler) item_callback, (XtPointer) item);
311 XtAddEventHandler(subj, ButtonPressMask, False, (XtEventHandler) item_callback, (XtPointer) item);
312 XtAddEventHandler(iv, ButtonPressMask, False, (XtEventHandler) item_callback, (XtPointer) item);
313 #endif
315 return 0;
318 int xutil_main(struct mail_list *lst, int argc, char **argv)
320 size_t n;
321 int rc;
322 struct applet app;
324 Widget vport; /* ViewPort (list container) */
325 Widget button;
326 Widget box;
328 rc = create_applet(&app, argc, argv);
330 vport = XtVaCreateManagedWidget("vport", viewportWidgetClass,
331 app.form,
332 XtNresizable, False,
333 XtNwidth, 450,
334 XtNheight, 400,
335 XtNallowVert, True, /* Display Vertical scrollbar */
336 XtNallowHoriz, True, /* Display Horizontal scrollbar */
337 XtNuseBottom, True, /* Horizontal scrollbar at bottom */
338 XtNtop, XtChainTop,
339 NULL);
340 if (vport == NULL)
341 return -1;
343 box = XtVaCreateManagedWidget("box", boxWidgetClass, vport,
344 XtNorientation, XtorientVertical,
345 NULL);
347 if (lst) {
348 for (n = 0; n < lst->n; n++) {
349 list_add_item(box, &lst->list[n]);
353 button = XtVaCreateManagedWidget("quit", commandWidgetClass,
354 app.form,
355 XtNfromVert, vport,
356 XtNleft, XtChainLeft,
357 XtNright, XtChainLeft,
358 XtNtop, XtChainBottom,
359 XtNbottom, XtChainBottom,
360 XtNresizable, False,
361 NULL);
363 XtAddCallback(button, XtNcallback, quit_callback, NULL);
365 XtRealizeWidget(app.shell);
367 /* Override default translations (A.k.a. bind actions to widgets) */
368 fix_scrollbar_translations(vport, "horizontal");
369 fix_scrollbar_translations(vport, "vertical");
371 XtAppMainLoop(app.ctx);
373 return 0;
376 /*======================================================================*/
377 /* END X11 CODE */
378 /*======================================================================*/
380 int main(int argc, char **argv)
382 int rc;
383 char old_path[PATH_MAX];
385 const char *path;
387 size_t n;
389 if (argc < 2) {
390 path = "/home/roberto/Maildir/cur/";
391 } else {
392 path = argv[1];
395 if (!getcwd(old_path, PATH_MAX)) {
396 fprintf(stderr, "%s - GETCWD: %s\n", __func__, strerror(errno));
397 return -1;
400 if (chdir(path) < 0) {
401 fprintf(stderr, "%s - CHDIR: %s\n", __func__, strerror(errno));
402 return -1;
405 n = dir_entries(path);
406 if (n < 1) {
407 fprintf(stderr, "%s is empty!\n", path);
408 rc = -1;
409 goto out_1;
412 /* Init mail list */
413 mlist = list_create(n, LIST_LIMIT);
414 if (!mlist) {
415 rc = -1;
416 goto out_1;
419 fprintf(stderr, "Try to process %lu files\n", (unsigned long) mlist->n);
421 rc = scan_dir(path, list_add_from_file);
423 /*int*/ xutil_main(mlist, argc, argv);
425 list_free(mlist);
427 out_1:
428 if (chdir(old_path) < 0) {
429 fprintf(stderr, "%s - Cannot restore path %s!\n", __func__, strerror(errno));
432 return rc;