Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / utils / test_header_osd.c
blob864d8d5350db3f2ae722f916246cc42c3b6b1bf0
1 #define _BSD_SOURCE
3 #include <stdio.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <errno.h>
7 #include <fcntl.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
12 #include <X11/Xlib.h>
13 #include <X11/Xutil.h>
14 #include <X11/Xatom.h>
15 #include <X11/Xft/Xft.h>
17 #include "string_utils.h"
18 #include "file_utils.h"
19 #include "rfc5322.h"
20 #include "list.h"
21 #include "md5_utils.h"
22 #include "osd.h"
23 #include "icons_utils.h"
25 static struct mail_entry mail;
27 void parse_file(const char *filename)
29 FILE *fh;
31 char *crlf = NULL;
33 char buf[1000];
35 fh = fopen(filename, "r");
36 if (!fh)
37 return;
39 fprintf(stderr, "-------------------------------------------\n");
40 fprintf(stderr, "Test file: %s\n", filename);
41 fprintf(stderr, "-------------------------------------------\n");
43 /* Scan all lines */
44 while (fgets(buf, sizeof(buf), fh)) {
45 if (!strncmp(buf, "From:", 5)) {
46 rfc5322_extract_address(mail.addr, buf, sizeof(mail.addr));
47 crlf = strrchr(buf, '\n');
48 if (crlf)
49 *crlf = '\0';
51 md5_calc_sum(mail.addr, mail.addr_md5);
53 } else if (!strncmp(buf, "Subject:", 8)) {
54 rfc5322_extract_subject(mail.subj, buf, sizeof(mail.subj));
55 } else {
56 #if 0
57 printf("[SKIP] ------- %s", buf);
58 #endif
62 fclose(fh);
65 static void print_mail(struct mail_entry *mail)
67 int i;
69 if (!mail)
70 return;
72 fprintf(stderr, "-------------------------------------------\n");
73 fprintf(stderr, "\"%s\" [", mail->addr);
74 for (i = 0; i < 16; i++) {
75 fprintf(stderr, "%02x", mail->addr_md5[i]);
77 fprintf(stderr, "] %s\n", mail->subj);
80 void display_osd(int argc, char **argv)
82 struct notify_object no;
84 char *icon = NULL;
86 Display *dpy;
87 XEvent e;
89 int running = 1;
91 /* Open connection with the server */
92 dpy = XOpenDisplay(NULL);
93 if (dpy == NULL) {
94 fprintf(stderr, "Cannot open display\n");
95 return;
98 icon = icon_get_path((char *) mail.addr_md5);
100 osd_init(dpy, &no, mail.addr, mail.subj, icon);
101 #if 0
102 if (osd_new_frame(argc, argv, &no)) {
103 goto out;
105 #endif
107 /* Select kind of events we are interested in */
108 XSelectInput(no.frame.display, no.frame.win,
109 ExposureMask | KeyPressMask | ButtonPressMask);
111 /* Create OSD object */
112 if (osd_new_notify_object(argc, argv, &no)) {
113 goto out;
116 /* Map (show) the window */
117 XMapWindow(no.frame.display, no.frame.win);
119 /* event loop */
120 while (running) {
121 XNextEvent(no.frame.display, &e);
123 switch (e.type) {
124 case Expose:
125 /* Draw or redraw the window */
126 osd_show_notify_object(&no);
127 break;
129 /* Exit on key press or mouse button press */
130 case KeyPress:
131 case ButtonPress:
132 running = 0;
133 break;
137 osd_delete_notify_object(&no);
139 out:
140 icon_free_path(icon);
142 /* Close connection to server */
143 XCloseDisplay(no.frame.display);
146 int main(int argc, char **argv)
148 const char *file;
150 if (argc < 2) {
151 fprintf(stderr, "%s <file>\n", argv[0]);
152 return -1;
155 file = argv[1];
157 if (file_type(file))
158 return -1;
160 parse_file(file);
162 print_mail(&mail);
164 display_osd(argc, argv);
166 return 0;