11 #include <sys/types.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>
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"
29 #include "icons_utils.h"
31 #include "iv_widget.h"
36 XtAppContext ctx
; /* Application context */
37 Widget shell
; /* Application shell widget */
38 Widget form
; /* Application top Form widget */
41 static struct mail_list
*mlist
;
43 #define FROM_ADDRESS_VALID (1 << 0)
44 #define REPLY_TO_VALID (1 << 1)
45 #define SUBJECT_VALID (1 << 2)
47 void list_add_from_file(const char *filename
)
51 char buf
[1024]; /* 998 + CR + LF + PADDING */
52 struct mail_entry entry
;
54 /* Check if is a regular file */
55 if (file_type(filename
)) {
59 fh
= fopen(filename
, "r");
63 while (fgets(buf
, sizeof(buf
), fh
)) {
64 if (!strncmp(buf
, "From:", 5)) {
65 if (!rfc5322_extract_address(entry
.addr
, buf
, 1024)) {
66 done
|= FROM_ADDRESS_VALID
;
68 } else if (!strncmp(buf
, "Reply-to:", 9)) {
69 if (done
& FROM_ADDRESS_VALID
)
72 if (!rfc5322_extract_address(entry
.addr
, buf
, 1024)) {
73 done
|= REPLY_TO_VALID
;
75 } else if (!strncmp(buf
, "Subject:", 8)) {
76 rfc5322_extract_subject(entry
.subj
, buf
, 1024);
77 done
|= SUBJECT_VALID
;
80 printf("[SKIP] ------- %s", buf
);
84 /* Exits loop if all fields are valid */
85 if (((done
& FROM_ADDRESS_VALID
) || (done
& REPLY_TO_VALID
)) &&
86 (done
& SUBJECT_VALID
)) {
93 strncpy(entry
.name
, filename
, 1024);
94 md5_calc_sum(entry
.addr
, entry
.addr_md5
);
97 if (list_add_entry(mlist
, &entry
)) {
98 fprintf(stderr
, "%s - No free slot for %s\n", __func__
, filename
);
100 /*fprintf(stderr, "%s - File %s added to list\n", __func__, filename);*/
106 /*======================================================================*/
108 /*======================================================================*/
110 /* The callback function for the quit button. */
111 void quit_callback(Widget w
,
112 __attribute__((unused
)) XtPointer client_data
,
113 __attribute__((unused
)) XtPointer call_data
)
117 XtDestroyApplicationContext(XtWidgetToApplicationContext(w
));
122 static void fix_scrollbar_translations(Widget w
, String name
)
124 Widget sb
= NULL
; /* scrollbar widget */
126 /* New scrollbar translation */
127 String trans
= "#override "
128 "<Btn1Down>: StartScroll(Forward)\n" /* Left button */
129 "<Btn2Down>: \n" /* Middle button */
130 "<Btn3Down>: StartScroll(Backward)\n" /* Right button */
131 "<Btn4Down>: StartScroll(Backward)\n" /* Wheel down */
132 "<Btn5Down>: StartScroll(Forward)\n" /* Wheel up */
133 "<Btn1Motion>: MoveThumb() NotifyThumb()\n"
134 "<BtnUp>: NotifyScroll(Proportional) EndScroll()";
139 sb
= XtNameToWidget(w
, name
);
141 XtOverrideTranslations(sb
, XtParseTranslationTable(trans
));
143 printf("%s: WARNING cannon set %s translation!\n", __func__
, name
);
147 static int create_applet(struct applet
*app
, int argc
, char **argv
)
149 XtSetLanguageProc(NULL
, (XtLanguageProc
) NULL
, NULL
);
151 /* Open Xt application shell widget */
152 app
->shell
= XtVaOpenApplication(&app
->ctx
, "Applet_X11_List_Mail", NULL
, 0,
154 applicationShellWidgetClass
,
157 /* Create top form widget */
158 app
->form
= XtVaCreateManagedWidget("AppForm", formWidgetClass
,
163 /* The callback function for the quit button. */
164 void item_callback(__attribute__((unused
)) Widget w
,
165 XtPointer item_string
,
166 __attribute__((unused
)) XtPointer call_data
)
168 fprintf(stderr
, "%s\n", (char *) item_string
);
171 static int list_add_item(Widget parent
, const struct mail_entry
*item
)
177 /* Depth of the root window */
189 d
= XtDisplay(parent
);
191 screen
= DefaultScreen(d
);
192 depth
= DisplayPlanes(d
, screen
);
193 visual
= XDefaultVisual(d
, screen
);
195 icon
= icon_get_path((char *) item
->addr_md5
);
197 box_h
= XtVaCreateManagedWidget("box_h", boxWidgetClass
, parent
,
198 XtNorientation
, XtorientHorizontal
,
201 pix
= create_ximage_from_PNG(d
, visual
, depth
, icon
, NULL
);
202 iv
= XtVaCreateManagedWidget("IV", imageViewWidgetClass
, box_h
,
207 box_v
= XtVaCreateManagedWidget("box_v", boxWidgetClass
, box_h
,
208 XtNorientation
, XtorientVertical
,
211 addr
= XtVaCreateManagedWidget("from", labelWidgetClass
, box_v
,
214 XtNlabel
, item
->addr
,
215 XtNjustify
, XtJustifyLeft
,
218 subj
= XtVaCreateManagedWidget("subj", labelWidgetClass
, box_v
,
221 XtNlabel
, item
->subj
,
222 XtNjustify
, XtJustifyLeft
,
225 /* Set handler for mouse button press on widgets
226 * Pass the file to the callbacks
232 * All the above ButtonPressMask
234 XtAddEventHandler(addr
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
->name
);
235 XtAddEventHandler(subj
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
->name
);
236 XtAddEventHandler(iv
, Button1Mask
, False
, (XtEventHandler
) item_callback
, (XtPointer
) item
->name
);
238 /* Free allocated icon pathname with 'icon_get_path()' */
239 icon_free_path(icon
);
244 int xutil_main(struct mail_list
*lst
, int argc
, char **argv
)
250 Widget vport
; /* ViewPort (list container) */
254 rc
= create_applet(&app
, argc
, argv
);
256 vport
= XtVaCreateManagedWidget("vport", viewportWidgetClass
,
261 XtNallowVert
, True
, /* Display Vertical scrollbar */
262 XtNallowHoriz
, True
, /* Display Horizontal scrollbar */
263 XtNuseBottom
, True
, /* Horizontal scrollbar at bottom */
269 box
= XtVaCreateManagedWidget("box", boxWidgetClass
, vport
,
270 XtNorientation
, XtorientVertical
,
274 for (n
= 0; n
< lst
->n
; n
++) {
275 list_add_item(box
, &lst
->list
[n
]);
279 button
= XtVaCreateManagedWidget("quit", commandWidgetClass
,
282 XtNleft
, XtChainLeft
,
283 XtNright
, XtChainLeft
,
284 XtNtop
, XtChainBottom
,
285 XtNbottom
, XtChainBottom
,
289 XtAddCallback(button
, XtNcallback
, quit_callback
, NULL
);
291 XtRealizeWidget(app
.shell
);
293 /* Override default translations (A.k.a. bind actions to widgets) */
294 fix_scrollbar_translations(vport
, "horizontal");
295 fix_scrollbar_translations(vport
, "vertical");
297 XtAppMainLoop(app
.ctx
);
302 /*======================================================================*/
304 /*======================================================================*/
306 int main(int argc
, char **argv
)
309 char old_path
[PATH_MAX
];
316 path
= "/home/roberto/Maildir/cur/";
321 if (!getcwd(old_path
, PATH_MAX
)) {
322 fprintf(stderr
, "%s - GETCWD: %s\n", __func__
, strerror(errno
));
326 if (chdir(path
) < 0) {
327 fprintf(stderr
, "%s - CHDIR: %s\n", __func__
, strerror(errno
));
331 n
= dir_entries(path
);
333 fprintf(stderr
, "%s is empty!\n", path
);
339 mlist
= list_create(n
, MESSAGE_LIMIT
);
345 fprintf(stderr
, "Try to process %lu files\n", (unsigned long) mlist
->n
);
347 rc
= scan_dir(path
, list_add_from_file
);
349 /*int*/ xutil_main(mlist
, argc
, argv
);
354 if (chdir(old_path
) < 0) {
355 fprintf(stderr
, "%s - Cannot restore path %s!\n", __func__
, strerror(errno
));