1 /* menu.c - show menu */
2 #include <stdlib.h>
3 #include <ncurses.h>
4 #include <unistd.h>
5 #include "screen.h"
6 #include "cfg.h"
7 #include "showlog.h"
8 #include "showoutb.h"
9 #include "menu.h"
10 
11 WINDOW *menuWin, *menuWinBoxed;
12 WINDOW *inputWin;
13 int menuIsActive;
14 
menuInit(int active)15 void menuInit(int active)
16 {
17   menuWinBoxed = newwin(LINES / 3, COLS, (LINES / 3) * 2, 0);
18   menuWin = newwin((LINES / 3) - 2, COLS - 4,
19 		   ((LINES / 3) * 2) + 1, 2);
20   inputWin = menuWin;
21   nodelay(inputWin, TRUE);
22   keypad(inputWin, TRUE);
23 
24   leaveok(menuWin, TRUE);
25   scrollok(menuWin, TRUE);
26 
27   menuIsActive = active;
28   menuForceDraw();
29 }
30 
menuSetActive()31 void menuSetActive()
32 {
33   menuIsActive = 1;
34   menuForceDraw();
35 }
36 
menuSetInactive()37 void menuSetInactive()
38 {
39   menuIsActive = 0;
40   menuForceDraw();
41 }
42 
menuDraw()43 void menuDraw()
44 {
45 //  int menuUpdated = 0;
46 
47 //  if (menuUpdated != 0) menuForceDraw();
48 }
49 
menuForceDraw()50 void menuForceDraw()
51 {
52   if (menuIsActive == 0)
53   {
54     wborder(menuWinBoxed, ACS_VLINE, ACS_VLINE, ACS_HLINE, ACS_HLINE,
55 	    ACS_ULCORNER, ACS_URCORNER, ACS_LLCORNER, ACS_LRCORNER);
56   }
57   else
58   {
59     wborder(menuWinBoxed, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK,
60 	    ACS_BLOCK, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK);
61   }
62   mvwprintw(menuWinBoxed, 0, (COLS - 6) / 2, " menu ");
63 
64   wmove(menuWin, 0, 0);
65 
66   wprintw(menuWin,
67 	  "<1> Poll boss\n"
68 	  "<2> Read mails\n"
69 	  "<q> quit\n"
70 	  "\n"
71 	  "Press <?> for help\n");
72 
73   wrefresh(menuWinBoxed);
74   wrefresh(menuWin);
75 }
76 
menuPollBoss()77 void menuPollBoss()
78 {
79   int rc;
80   char *cmdline;
81   char *addr;
82   char rfcAddr[30];
83 
84   if (cfg.pollCmd != NULL)
85   {
86     if (fork() == 0) // do it in background
87     {
88       addr = aka2str(cfg.bossAddr);
89       sprintf(rfcAddr, "f%d.n%d.z%d", cfg.bossAddr.node, cfg.bossAddr.net,
90 	      cfg.bossAddr.zone);
91       cmdline = malloc(strlen(cfg.pollCmd)+strlen(addr)+strlen(rfcAddr)+1);
92       sprintf(cmdline, cfg.pollCmd, addr, rfcAddr);
93       rc = system(cmdline);
94       free(cmdline);
95 
96       exit(0);
97     }
98 
99     clearok(menuWinBoxed, TRUE);
100     menuForceDraw();
101     logForceDraw();
102     outbForceDraw();
103   }
104 }
105 
menuReadMails()106 void menuReadMails()
107 {
108   int rc;
109 
110   if (cfg.mailEditor != NULL)
111   {
112     menuDone();
113     outbDone();
114     logDone();
115     scrDone();
116 
117     rc = system(cfg.mailEditor);
118 
119     scrInit();
120     logInit(0);
121     outbInit(0);
122     menuInit(1);
123   }
124 }
125 
menuShowHelp()126 void menuShowHelp()
127 {
128   WINDOW *helpWinBoxed, *helpWin;
129 
130   helpWinBoxed = newwin(LINES, COLS, 0, 0);
131   helpWin = newwin(LINES - 2, COLS - 4, 1, 2);
132 
133   wborder(helpWinBoxed, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK,
134 	  ACS_BLOCK, ACS_BLOCK, ACS_BLOCK, ACS_BLOCK);
135   mvwprintw(helpWinBoxed, 0, (COLS - 11) / 2, " menu help ");
136 
137   mvwprintw(helpWin, 0, 0,
138 	    "\n"
139 	    "<1>           poll your boss node for mails\n"
140 	    "<2>           read and write mails and export them\n"
141 	    "\n"
142 	    "<?>           help (this screen)\n"
143 	    "<Tab>         cycle through windows\n"
144 	    "\n"
145 	    "<q>           quit\n"
146 	    "\n"
147 	    "Press any key to continue\n");
148 
149   wrefresh(helpWinBoxed);
150   wrefresh(helpWin);
151 
152   wgetch(helpWin);
153 
154   delwin(helpWin);
155   delwin(helpWinBoxed);
156 
157   touchwin(stdscr); wrefresh(stdscr);
158   logForceDraw();
159   outbForceDraw();
160   menuForceDraw();
161 }
162 
menuProcessKey(int key)163 void menuProcessKey(int key)
164 {
165   switch (key)
166   {
167   case '1':
168     menuPollBoss();
169     break;
170 
171   case '2':
172     menuReadMails();
173     break;
174 
175   case '?':
176     menuShowHelp();
177     break;
178   }
179 }
180 
menuDone()181 void menuDone()
182 {
183   delwin(menuWin);
184   delwin(menuWinBoxed);
185 }
186 
187