1 | #include "owl.h" |
---|
2 | #include <stdio.h> |
---|
3 | #include <sys/ioctl.h> |
---|
4 | |
---|
5 | static void _owl_global_init_windows(owl_global *g); |
---|
6 | |
---|
7 | void owl_global_init(owl_global *g) { |
---|
8 | char *cd; |
---|
9 | const char *homedir; |
---|
10 | |
---|
11 | g_type_init(); |
---|
12 | g_thread_init(NULL); |
---|
13 | |
---|
14 | owl_select_init(); |
---|
15 | |
---|
16 | g->lines=LINES; |
---|
17 | g->cols=COLS; |
---|
18 | /* We shouldn't need this if we initialize lines and cols before the first |
---|
19 | * owl_window_get_screen, but to be safe, we synchronize. */ |
---|
20 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
---|
21 | |
---|
22 | g->context_stack = NULL; |
---|
23 | owl_global_push_context(g, OWL_CTX_STARTUP, NULL, NULL, NULL); |
---|
24 | |
---|
25 | g->curmsg=0; |
---|
26 | g->topmsg=0; |
---|
27 | g->markedmsgid=-1; |
---|
28 | g->startupargs=NULL; |
---|
29 | |
---|
30 | owl_variable_dict_setup(&(g->vars)); |
---|
31 | |
---|
32 | g->rightshift=0; |
---|
33 | |
---|
34 | g->pw = NULL; |
---|
35 | g->vw = NULL; |
---|
36 | g->tw = NULL; |
---|
37 | |
---|
38 | owl_keyhandler_init(&g->kh); |
---|
39 | owl_keys_setup_keymaps(&g->kh); |
---|
40 | |
---|
41 | owl_dict_create(&(g->filters)); |
---|
42 | g->filterlist = NULL; |
---|
43 | g->puntlist = g_ptr_array_new(); |
---|
44 | g->messagequeue = g_queue_new(); |
---|
45 | owl_dict_create(&(g->styledict)); |
---|
46 | g->curmsg_vert_offset=0; |
---|
47 | g->resizepending=0; |
---|
48 | g->direction=OWL_DIRECTION_DOWNWARDS; |
---|
49 | owl_fmtext_init_colorpair_mgr(&(g->cpmgr)); |
---|
50 | g->debug=OWL_DEBUG; |
---|
51 | owl_regex_init(&g->search_re); |
---|
52 | g->starttime=time(NULL); /* assumes we call init only a start time */ |
---|
53 | g->lastinputtime=g->starttime; |
---|
54 | g->newmsgproc_pid=0; |
---|
55 | |
---|
56 | owl_global_set_config_format(g, 0); |
---|
57 | owl_global_set_no_have_config(g); |
---|
58 | owl_history_init(&(g->msghist)); |
---|
59 | owl_history_init(&(g->cmdhist)); |
---|
60 | g->nextmsgid=0; |
---|
61 | |
---|
62 | /* Fill in some variables which don't have constant defaults */ |
---|
63 | |
---|
64 | /* glib's g_get_home_dir prefers passwd entries to $HOME, so we |
---|
65 | * explicitly check getenv first. */ |
---|
66 | homedir = getenv("HOME"); |
---|
67 | if (!homedir) |
---|
68 | homedir = g_get_home_dir(); |
---|
69 | g->homedir = g_strdup(homedir); |
---|
70 | |
---|
71 | g->confdir = NULL; |
---|
72 | g->startupfile = NULL; |
---|
73 | cd = g_build_filename(g->homedir, OWL_CONFIG_DIR, NULL); |
---|
74 | owl_global_set_confdir(g, cd); |
---|
75 | g_free(cd); |
---|
76 | |
---|
77 | g->msglist = owl_messagelist_new(); |
---|
78 | |
---|
79 | _owl_global_init_windows(g); |
---|
80 | |
---|
81 | g->aim_screenname=NULL; |
---|
82 | g->aim_screenname_for_filters=NULL; |
---|
83 | g->aim_loggedin=0; |
---|
84 | owl_buddylist_init(&(g->buddylist)); |
---|
85 | |
---|
86 | g->havezephyr=0; |
---|
87 | g->haveaim=0; |
---|
88 | g->ignoreaimlogin=0; |
---|
89 | owl_global_set_no_doaimevents(g); |
---|
90 | |
---|
91 | owl_errqueue_init(&(g->errqueue)); |
---|
92 | |
---|
93 | owl_zbuddylist_create(&(g->zbuddies)); |
---|
94 | |
---|
95 | g->zaldlist = NULL; |
---|
96 | g->pseudologin_notify = 0; |
---|
97 | |
---|
98 | owl_message_init_fmtext_cache(); |
---|
99 | g->kill_buffer = NULL; |
---|
100 | |
---|
101 | g->interrupt_count = 0; |
---|
102 | #if GLIB_CHECK_VERSION(2, 31, 0) |
---|
103 | g_mutex_init(&g->interrupt_lock); |
---|
104 | #else |
---|
105 | g->interrupt_lock = g_mutex_new(); |
---|
106 | #endif |
---|
107 | } |
---|
108 | |
---|
109 | static void _owl_global_init_windows(owl_global *g) |
---|
110 | { |
---|
111 | /* Create the main window */ |
---|
112 | owl_mainpanel_init(&(g->mainpanel)); |
---|
113 | |
---|
114 | /* Create the widgets */ |
---|
115 | g->mw = owl_mainwin_new(g->mainpanel.recwin); |
---|
116 | owl_msgwin_init(&(g->msgwin), g->mainpanel.msgwin); |
---|
117 | owl_sepbar_init(g->mainpanel.sepwin); |
---|
118 | |
---|
119 | owl_window_set_default_cursor(g->mainpanel.sepwin); |
---|
120 | |
---|
121 | /* set up a pad for input */ |
---|
122 | g->input_pad = newpad(1, 1); |
---|
123 | nodelay(g->input_pad, 1); |
---|
124 | keypad(g->input_pad, 1); |
---|
125 | meta(g->input_pad, 1); |
---|
126 | } |
---|
127 | |
---|
128 | void owl_global_sepbar_dirty(owl_global *g) |
---|
129 | { |
---|
130 | owl_window_dirty(g->mainpanel.sepwin); |
---|
131 | } |
---|
132 | |
---|
133 | /* Called once perl has been initialized */ |
---|
134 | void owl_global_complete_setup(owl_global *g) |
---|
135 | { |
---|
136 | owl_cmddict_setup(&(g->cmds)); |
---|
137 | } |
---|
138 | |
---|
139 | owl_context *owl_global_get_context(const owl_global *g) { |
---|
140 | if (!g->context_stack) |
---|
141 | return NULL; |
---|
142 | return g->context_stack->data; |
---|
143 | } |
---|
144 | |
---|
145 | static void owl_global_activate_context(owl_global *g, owl_context *c) { |
---|
146 | if (!c) |
---|
147 | return; |
---|
148 | |
---|
149 | if (c->keymap) { |
---|
150 | if (!owl_keyhandler_activate(owl_global_get_keyhandler(g), c->keymap)) { |
---|
151 | owl_function_error("Unable to activate keymap '%s'", c->keymap); |
---|
152 | } |
---|
153 | } |
---|
154 | owl_window_set_cursor(c->cursor); |
---|
155 | } |
---|
156 | |
---|
157 | void owl_global_push_context(owl_global *g, int mode, void *data, const char *keymap, owl_window *cursor) { |
---|
158 | owl_context *c; |
---|
159 | c = owl_context_new(mode, data, keymap, cursor); |
---|
160 | owl_global_push_context_obj(g, c); |
---|
161 | } |
---|
162 | |
---|
163 | void owl_global_push_context_obj(owl_global *g, owl_context *c) |
---|
164 | { |
---|
165 | g->context_stack = g_list_prepend(g->context_stack, c); |
---|
166 | owl_global_activate_context(g, owl_global_get_context(g)); |
---|
167 | } |
---|
168 | |
---|
169 | /* Pops the current context from the context stack and returns it. Caller is |
---|
170 | * responsible for freeing. */ |
---|
171 | CALLER_OWN owl_context *owl_global_pop_context_no_delete(owl_global *g) |
---|
172 | { |
---|
173 | owl_context *c; |
---|
174 | if (!g->context_stack) |
---|
175 | return NULL; |
---|
176 | c = owl_global_get_context(g); |
---|
177 | owl_context_deactivate(c); |
---|
178 | g->context_stack = g_list_delete_link(g->context_stack, |
---|
179 | g->context_stack); |
---|
180 | owl_global_activate_context(g, owl_global_get_context(g)); |
---|
181 | return c; |
---|
182 | } |
---|
183 | |
---|
184 | /* Pops the current context from the context stack and deletes it. */ |
---|
185 | void owl_global_pop_context(owl_global *g) { |
---|
186 | owl_context *c; |
---|
187 | c = owl_global_pop_context_no_delete(g); |
---|
188 | if (c) |
---|
189 | owl_context_delete(c); |
---|
190 | } |
---|
191 | |
---|
192 | int owl_global_get_lines(const owl_global *g) { |
---|
193 | return(g->lines); |
---|
194 | } |
---|
195 | |
---|
196 | int owl_global_get_cols(const owl_global *g) { |
---|
197 | return(g->cols); |
---|
198 | } |
---|
199 | |
---|
200 | int owl_global_get_recwin_lines(const owl_global *g) { |
---|
201 | return g->mainpanel.recwinlines; |
---|
202 | } |
---|
203 | |
---|
204 | /* curmsg */ |
---|
205 | |
---|
206 | int owl_global_get_curmsg(const owl_global *g) { |
---|
207 | return(g->curmsg); |
---|
208 | } |
---|
209 | |
---|
210 | void owl_global_set_curmsg(owl_global *g, int i) { |
---|
211 | g->curmsg=i; |
---|
212 | /* we will reset the vertical offset from here */ |
---|
213 | /* we might want to move this out to the functions later */ |
---|
214 | owl_global_set_curmsg_vert_offset(g, 0); |
---|
215 | } |
---|
216 | |
---|
217 | /* topmsg */ |
---|
218 | |
---|
219 | int owl_global_get_topmsg(const owl_global *g) { |
---|
220 | return(g->topmsg); |
---|
221 | } |
---|
222 | |
---|
223 | void owl_global_set_topmsg(owl_global *g, int i) { |
---|
224 | g->topmsg=i; |
---|
225 | } |
---|
226 | |
---|
227 | /* markedmsgid */ |
---|
228 | |
---|
229 | int owl_global_get_markedmsgid(const owl_global *g) { |
---|
230 | return(g->markedmsgid); |
---|
231 | } |
---|
232 | |
---|
233 | void owl_global_set_markedmsgid(owl_global *g, int i) { |
---|
234 | g->markedmsgid=i; |
---|
235 | /* i; index of message in the current view. |
---|
236 | const owl_message *m; |
---|
237 | owl_view *v; |
---|
238 | |
---|
239 | v = owl_global_get_current_view(&g); |
---|
240 | m = owl_view_get_element(v, i); |
---|
241 | g->markedmsgid = m ? owl_message_get_id(m) : 0; |
---|
242 | */ |
---|
243 | } |
---|
244 | |
---|
245 | /* windows */ |
---|
246 | |
---|
247 | owl_mainwin *owl_global_get_mainwin(owl_global *g) |
---|
248 | { |
---|
249 | return g->mw; |
---|
250 | } |
---|
251 | |
---|
252 | owl_popwin *owl_global_get_popwin(owl_global *g) { |
---|
253 | return g->pw; |
---|
254 | } |
---|
255 | |
---|
256 | void owl_global_set_popwin(owl_global *g, owl_popwin *pw) { |
---|
257 | g->pw = pw; |
---|
258 | } |
---|
259 | |
---|
260 | /* msglist */ |
---|
261 | |
---|
262 | owl_messagelist *owl_global_get_msglist(owl_global *g) { |
---|
263 | return g->msglist; |
---|
264 | } |
---|
265 | |
---|
266 | /* keyhandler */ |
---|
267 | |
---|
268 | owl_keyhandler *owl_global_get_keyhandler(owl_global *g) { |
---|
269 | return(&(g->kh)); |
---|
270 | } |
---|
271 | |
---|
272 | /* Gets the currently active typwin out of the current context. */ |
---|
273 | owl_editwin *owl_global_current_typwin(const owl_global *g) { |
---|
274 | owl_context *ctx = owl_global_get_context(g); |
---|
275 | return owl_editcontext_get_editwin(ctx); |
---|
276 | } |
---|
277 | |
---|
278 | /* variable dictionary */ |
---|
279 | |
---|
280 | owl_vardict *owl_global_get_vardict(owl_global *g) { |
---|
281 | return &(g->vars); |
---|
282 | } |
---|
283 | |
---|
284 | /* command dictionary */ |
---|
285 | |
---|
286 | owl_cmddict *owl_global_get_cmddict(owl_global *g) { |
---|
287 | return &(g->cmds); |
---|
288 | } |
---|
289 | |
---|
290 | /* rightshift */ |
---|
291 | |
---|
292 | void owl_global_set_rightshift(owl_global *g, int i) { |
---|
293 | g->rightshift = i; |
---|
294 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
---|
295 | } |
---|
296 | |
---|
297 | int owl_global_get_rightshift(const owl_global *g) { |
---|
298 | return(g->rightshift); |
---|
299 | } |
---|
300 | |
---|
301 | /* typwin */ |
---|
302 | |
---|
303 | owl_editwin *owl_global_set_typwin_active(owl_global *g, int style, owl_history *hist) { |
---|
304 | int d; |
---|
305 | d = owl_global_get_typewindelta(g); |
---|
306 | if (d > 0 && style == OWL_EDITWIN_STYLE_MULTILINE) |
---|
307 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) + d); |
---|
308 | |
---|
309 | if (g->typwin_erase_id) { |
---|
310 | g_signal_handler_disconnect(g->mainpanel.typwin, g->typwin_erase_id); |
---|
311 | g->typwin_erase_id = 0; |
---|
312 | } |
---|
313 | |
---|
314 | g->tw = owl_editwin_new(g->mainpanel.typwin, |
---|
315 | owl_global_get_typwin_lines(g), |
---|
316 | g->cols, |
---|
317 | style, |
---|
318 | hist); |
---|
319 | return g->tw; |
---|
320 | } |
---|
321 | |
---|
322 | void owl_global_deactivate_editcontext(owl_context *ctx) { |
---|
323 | owl_global *g = ctx->cbdata; |
---|
324 | owl_global_set_typwin_inactive(g); |
---|
325 | } |
---|
326 | |
---|
327 | void owl_global_set_typwin_inactive(owl_global *g) { |
---|
328 | int d = owl_global_get_typewindelta(g); |
---|
329 | if (d > 0 && owl_editwin_get_style(g->tw) == OWL_EDITWIN_STYLE_MULTILINE) |
---|
330 | owl_function_resize_typwin(owl_global_get_typwin_lines(g) - d); |
---|
331 | |
---|
332 | if (!g->typwin_erase_id) { |
---|
333 | g->typwin_erase_id = |
---|
334 | g_signal_connect(g->mainpanel.typwin, "redraw", G_CALLBACK(owl_window_erase_cb), NULL); |
---|
335 | } |
---|
336 | owl_window_dirty(g->mainpanel.typwin); |
---|
337 | |
---|
338 | owl_editwin_unref(g->tw); |
---|
339 | g->tw = NULL; |
---|
340 | } |
---|
341 | |
---|
342 | /* resize */ |
---|
343 | |
---|
344 | void owl_global_set_resize_pending(owl_global *g) { |
---|
345 | g->resizepending = true; |
---|
346 | } |
---|
347 | |
---|
348 | const char *owl_global_get_homedir(const owl_global *g) { |
---|
349 | if (g->homedir) return(g->homedir); |
---|
350 | return("/"); |
---|
351 | } |
---|
352 | |
---|
353 | const char *owl_global_get_confdir(const owl_global *g) { |
---|
354 | if (g->confdir) return(g->confdir); |
---|
355 | return("/"); |
---|
356 | } |
---|
357 | |
---|
358 | /* |
---|
359 | * Setting this also sets startupfile to confdir/startup |
---|
360 | */ |
---|
361 | void owl_global_set_confdir(owl_global *g, const char *cd) { |
---|
362 | g_free(g->confdir); |
---|
363 | g->confdir = g_strdup(cd); |
---|
364 | g_free(g->startupfile); |
---|
365 | g->startupfile = g_build_filename(cd, "startup", NULL); |
---|
366 | } |
---|
367 | |
---|
368 | const char *owl_global_get_startupfile(const owl_global *g) { |
---|
369 | if(g->startupfile) return(g->startupfile); |
---|
370 | return("/"); |
---|
371 | } |
---|
372 | |
---|
373 | int owl_global_get_direction(const owl_global *g) { |
---|
374 | return(g->direction); |
---|
375 | } |
---|
376 | |
---|
377 | void owl_global_set_direction_downwards(owl_global *g) { |
---|
378 | g->direction=OWL_DIRECTION_DOWNWARDS; |
---|
379 | } |
---|
380 | |
---|
381 | void owl_global_set_direction_upwards(owl_global *g) { |
---|
382 | g->direction=OWL_DIRECTION_UPWARDS; |
---|
383 | } |
---|
384 | |
---|
385 | /* perl stuff */ |
---|
386 | |
---|
387 | void owl_global_set_perlinterp(owl_global *g, void *p) { |
---|
388 | g->perl=p; |
---|
389 | } |
---|
390 | |
---|
391 | void *owl_global_get_perlinterp(const owl_global *g) { |
---|
392 | return(g->perl); |
---|
393 | } |
---|
394 | |
---|
395 | int owl_global_is_config_format(const owl_global *g) { |
---|
396 | if (g->config_format) return(1); |
---|
397 | return(0); |
---|
398 | } |
---|
399 | |
---|
400 | void owl_global_set_config_format(owl_global *g, int state) { |
---|
401 | if (state==1) { |
---|
402 | g->config_format=1; |
---|
403 | } else { |
---|
404 | g->config_format=0; |
---|
405 | } |
---|
406 | } |
---|
407 | |
---|
408 | void owl_global_set_have_config(owl_global *g) { |
---|
409 | g->haveconfig=1; |
---|
410 | } |
---|
411 | |
---|
412 | void owl_global_set_no_have_config(owl_global *g) { |
---|
413 | g->haveconfig=0; |
---|
414 | } |
---|
415 | |
---|
416 | int owl_global_have_config(owl_global *g) { |
---|
417 | if (g->haveconfig) return(1); |
---|
418 | return(0); |
---|
419 | } |
---|
420 | |
---|
421 | /* |
---|
422 | * Compute the size of the terminal. Try a ioctl, fallback to other stuff on |
---|
423 | * fail. |
---|
424 | */ |
---|
425 | void owl_global_get_terminal_size(int *lines, int *cols) { |
---|
426 | struct winsize size; |
---|
427 | /* get the new size */ |
---|
428 | ioctl(STDIN_FILENO, TIOCGWINSZ, &size); |
---|
429 | if (size.ws_row) { |
---|
430 | *lines = size.ws_row; |
---|
431 | } else { |
---|
432 | *lines = LINES; |
---|
433 | } |
---|
434 | |
---|
435 | if (size.ws_col) { |
---|
436 | *cols = size.ws_col; |
---|
437 | } else { |
---|
438 | *cols = COLS; |
---|
439 | } |
---|
440 | } |
---|
441 | |
---|
442 | void owl_global_check_resize(owl_global *g) { |
---|
443 | /* resize the screen. If lines or cols is 0 use the terminal size */ |
---|
444 | if (!g->resizepending) return; |
---|
445 | g->resizepending = false; |
---|
446 | |
---|
447 | owl_global_get_terminal_size(&g->lines, &g->cols); |
---|
448 | owl_window_resize(owl_window_get_screen(), g->lines, g->cols); |
---|
449 | |
---|
450 | owl_function_debugmsg("New size is %i lines, %i cols.", g->lines, g->cols); |
---|
451 | } |
---|
452 | |
---|
453 | /* debug */ |
---|
454 | |
---|
455 | int owl_global_is_debug_fast(const owl_global *g) { |
---|
456 | if (g->debug) return(1); |
---|
457 | return(0); |
---|
458 | } |
---|
459 | |
---|
460 | /* starttime */ |
---|
461 | |
---|
462 | time_t owl_global_get_starttime(const owl_global *g) { |
---|
463 | return(g->starttime); |
---|
464 | } |
---|
465 | |
---|
466 | time_t owl_global_get_runtime(const owl_global *g) { |
---|
467 | return(time(NULL)-g->starttime); |
---|
468 | } |
---|
469 | |
---|
470 | time_t owl_global_get_lastinputtime(const owl_global *g) { |
---|
471 | return(g->lastinputtime); |
---|
472 | } |
---|
473 | |
---|
474 | void owl_global_set_lastinputtime(owl_global *g, time_t time) { |
---|
475 | g->lastinputtime = time; |
---|
476 | } |
---|
477 | |
---|
478 | time_t owl_global_get_idletime(const owl_global *g) { |
---|
479 | return(time(NULL)-g->lastinputtime); |
---|
480 | } |
---|
481 | |
---|
482 | /* viewwin */ |
---|
483 | |
---|
484 | owl_viewwin *owl_global_get_viewwin(owl_global *g) { |
---|
485 | return g->vw; |
---|
486 | } |
---|
487 | |
---|
488 | void owl_global_set_viewwin(owl_global *g, owl_viewwin *vw) { |
---|
489 | g->vw = vw; |
---|
490 | } |
---|
491 | |
---|
492 | |
---|
493 | /* vert offset */ |
---|
494 | |
---|
495 | int owl_global_get_curmsg_vert_offset(const owl_global *g) { |
---|
496 | return(g->curmsg_vert_offset); |
---|
497 | } |
---|
498 | |
---|
499 | void owl_global_set_curmsg_vert_offset(owl_global *g, int i) { |
---|
500 | g->curmsg_vert_offset = i; |
---|
501 | } |
---|
502 | |
---|
503 | /* startup args */ |
---|
504 | |
---|
505 | void owl_global_set_startupargs(owl_global *g, int argc, char **argv) { |
---|
506 | g_free(g->startupargs); |
---|
507 | g->startupargs = g_strjoinv(" ", argv); |
---|
508 | } |
---|
509 | |
---|
510 | const char *owl_global_get_startupargs(const owl_global *g) { |
---|
511 | if (g->startupargs) return(g->startupargs); |
---|
512 | return(""); |
---|
513 | } |
---|
514 | |
---|
515 | /* history */ |
---|
516 | |
---|
517 | owl_history *owl_global_get_msg_history(owl_global *g) { |
---|
518 | return(&(g->msghist)); |
---|
519 | } |
---|
520 | |
---|
521 | owl_history *owl_global_get_cmd_history(owl_global *g) { |
---|
522 | return(&(g->cmdhist)); |
---|
523 | } |
---|
524 | |
---|
525 | /* filterlist */ |
---|
526 | typedef struct _owl_global_filter_ent { /* noproto */ |
---|
527 | owl_global *g; |
---|
528 | owl_filter *f; |
---|
529 | } owl_global_filter_ent; |
---|
530 | |
---|
531 | owl_filter *owl_global_get_filter(const owl_global *g, const char *name) { |
---|
532 | owl_global_filter_ent *e = owl_dict_find_element(&(g->filters), name); |
---|
533 | if (e) return e->f; |
---|
534 | return NULL; |
---|
535 | } |
---|
536 | |
---|
537 | static void owl_global_delete_filter_ent(void *data) |
---|
538 | { |
---|
539 | owl_global_filter_ent *e = data; |
---|
540 | e->g->filterlist = g_list_remove(e->g->filterlist, e->f); |
---|
541 | owl_filter_delete(e->f); |
---|
542 | g_free(e); |
---|
543 | } |
---|
544 | |
---|
545 | void owl_global_add_filter(owl_global *g, owl_filter *f) { |
---|
546 | owl_global_filter_ent *e = g_new(owl_global_filter_ent, 1); |
---|
547 | e->g = g; |
---|
548 | e->f = f; |
---|
549 | |
---|
550 | owl_dict_insert_element(&(g->filters), owl_filter_get_name(f), |
---|
551 | e, owl_global_delete_filter_ent); |
---|
552 | g->filterlist = g_list_append(g->filterlist, f); |
---|
553 | } |
---|
554 | |
---|
555 | void owl_global_remove_filter(owl_global *g, const char *name) { |
---|
556 | owl_global_filter_ent *e = owl_dict_remove_element(&(g->filters), name); |
---|
557 | if (e) |
---|
558 | owl_global_delete_filter_ent(e); |
---|
559 | } |
---|
560 | |
---|
561 | /* nextmsgid */ |
---|
562 | |
---|
563 | int owl_global_get_nextmsgid(owl_global *g) { |
---|
564 | return(g->nextmsgid++); |
---|
565 | } |
---|
566 | |
---|
567 | /* current view */ |
---|
568 | |
---|
569 | owl_view *owl_global_get_current_view(owl_global *g) { |
---|
570 | return(&(g->current_view)); |
---|
571 | } |
---|
572 | |
---|
573 | owl_colorpair_mgr *owl_global_get_colorpair_mgr(owl_global *g) { |
---|
574 | return(&(g->cpmgr)); |
---|
575 | } |
---|
576 | |
---|
577 | /* puntlist */ |
---|
578 | |
---|
579 | GPtrArray *owl_global_get_puntlist(owl_global *g) { |
---|
580 | return g->puntlist; |
---|
581 | } |
---|
582 | |
---|
583 | int owl_global_message_is_puntable(owl_global *g, const owl_message *m) { |
---|
584 | const GPtrArray *pl; |
---|
585 | int i; |
---|
586 | |
---|
587 | pl = owl_global_get_puntlist(g); |
---|
588 | for (i = 0; i < pl->len; i++) { |
---|
589 | if (owl_filter_message_match(pl->pdata[i], m)) return 1; |
---|
590 | } |
---|
591 | return 0; |
---|
592 | } |
---|
593 | |
---|
594 | int owl_global_should_followlast(owl_global *g) { |
---|
595 | const owl_view *v; |
---|
596 | |
---|
597 | if (!owl_global_is__followlast(g)) return(0); |
---|
598 | |
---|
599 | v=owl_global_get_current_view(g); |
---|
600 | |
---|
601 | if (owl_global_get_curmsg(g)==owl_view_get_size(v)-1) return(1); |
---|
602 | return(0); |
---|
603 | } |
---|
604 | |
---|
605 | int owl_global_is_search_active(const owl_global *g) { |
---|
606 | if (owl_regex_is_set(&g->search_re)) return(1); |
---|
607 | return(0); |
---|
608 | } |
---|
609 | |
---|
610 | void owl_global_set_search_re(owl_global *g, const owl_regex *re) { |
---|
611 | if (owl_regex_is_set(&g->search_re)) { |
---|
612 | owl_regex_cleanup(&g->search_re); |
---|
613 | owl_regex_init(&g->search_re); |
---|
614 | } |
---|
615 | if (re != NULL) |
---|
616 | owl_regex_copy(re, &g->search_re); |
---|
617 | /* TODO: Emit a signal so we don't depend on the viewwin and mainwin */ |
---|
618 | if (owl_global_get_viewwin(g)) |
---|
619 | owl_viewwin_dirty(owl_global_get_viewwin(g)); |
---|
620 | owl_mainwin_redisplay(owl_global_get_mainwin(g)); |
---|
621 | } |
---|
622 | |
---|
623 | const owl_regex *owl_global_get_search_re(const owl_global *g) { |
---|
624 | return &g->search_re; |
---|
625 | } |
---|
626 | |
---|
627 | void owl_global_set_newmsgproc_pid(owl_global *g, pid_t i) { |
---|
628 | g->newmsgproc_pid=i; |
---|
629 | } |
---|
630 | |
---|
631 | pid_t owl_global_get_newmsgproc_pid(const owl_global *g) { |
---|
632 | return(g->newmsgproc_pid); |
---|
633 | } |
---|
634 | |
---|
635 | /* AIM stuff */ |
---|
636 | |
---|
637 | int owl_global_is_aimloggedin(const owl_global *g) |
---|
638 | { |
---|
639 | if (g->aim_loggedin) return(1); |
---|
640 | return(0); |
---|
641 | } |
---|
642 | |
---|
643 | const char *owl_global_get_aim_screenname(const owl_global *g) |
---|
644 | { |
---|
645 | if (owl_global_is_aimloggedin(g)) { |
---|
646 | return (g->aim_screenname); |
---|
647 | } |
---|
648 | return(""); |
---|
649 | } |
---|
650 | |
---|
651 | const char *owl_global_get_aim_screenname_for_filters(const owl_global *g) |
---|
652 | { |
---|
653 | if (owl_global_is_aimloggedin(g)) { |
---|
654 | return (g->aim_screenname_for_filters); |
---|
655 | } |
---|
656 | return(""); |
---|
657 | } |
---|
658 | |
---|
659 | void owl_global_set_aimloggedin(owl_global *g, const char *screenname) |
---|
660 | { |
---|
661 | char *sn_escaped; |
---|
662 | g->aim_loggedin=1; |
---|
663 | if (g->aim_screenname) g_free(g->aim_screenname); |
---|
664 | if (g->aim_screenname_for_filters) g_free(g->aim_screenname_for_filters); |
---|
665 | g->aim_screenname=g_strdup(screenname); |
---|
666 | sn_escaped = owl_text_quote(screenname, OWL_REGEX_QUOTECHARS, OWL_REGEX_QUOTEWITH); |
---|
667 | g->aim_screenname_for_filters = owl_arg_quote(sn_escaped); |
---|
668 | g_free(sn_escaped); |
---|
669 | } |
---|
670 | |
---|
671 | void owl_global_set_aimnologgedin(owl_global *g) |
---|
672 | { |
---|
673 | g->aim_loggedin=0; |
---|
674 | } |
---|
675 | |
---|
676 | bool owl_global_is_doaimevents(const owl_global *g) |
---|
677 | { |
---|
678 | return g->aim_event_source != NULL; |
---|
679 | } |
---|
680 | |
---|
681 | void owl_global_set_doaimevents(owl_global *g) |
---|
682 | { |
---|
683 | if (g->aim_event_source) |
---|
684 | return; |
---|
685 | g->aim_event_source = owl_aim_event_source_new(owl_global_get_aimsess(g)); |
---|
686 | g_source_attach(g->aim_event_source, NULL); |
---|
687 | } |
---|
688 | |
---|
689 | void owl_global_set_no_doaimevents(owl_global *g) |
---|
690 | { |
---|
691 | if (!g->aim_event_source) |
---|
692 | return; |
---|
693 | g_source_destroy(g->aim_event_source); |
---|
694 | g_source_unref(g->aim_event_source); |
---|
695 | g->aim_event_source = NULL; |
---|
696 | } |
---|
697 | |
---|
698 | aim_session_t *owl_global_get_aimsess(owl_global *g) |
---|
699 | { |
---|
700 | return(&(g->aimsess)); |
---|
701 | } |
---|
702 | |
---|
703 | aim_conn_t *owl_global_get_bosconn(owl_global *g) |
---|
704 | { |
---|
705 | return(&(g->bosconn)); |
---|
706 | } |
---|
707 | |
---|
708 | void owl_global_set_bossconn(owl_global *g, aim_conn_t *conn) |
---|
709 | { |
---|
710 | g->bosconn=*conn; |
---|
711 | } |
---|
712 | |
---|
713 | /* message queue */ |
---|
714 | |
---|
715 | void owl_global_messagequeue_addmsg(owl_global *g, owl_message *m) |
---|
716 | { |
---|
717 | g_queue_push_tail(g->messagequeue, m); |
---|
718 | } |
---|
719 | |
---|
720 | /* pop off the first message and return it. Return NULL if the queue |
---|
721 | * is empty. The caller should free the message after using it, if |
---|
722 | * necessary. |
---|
723 | */ |
---|
724 | CALLER_OWN owl_message *owl_global_messagequeue_popmsg(owl_global *g) |
---|
725 | { |
---|
726 | owl_message *out; |
---|
727 | |
---|
728 | if (g_queue_is_empty(g->messagequeue)) |
---|
729 | return NULL; |
---|
730 | out = g_queue_pop_head(g->messagequeue); |
---|
731 | return out; |
---|
732 | } |
---|
733 | |
---|
734 | int owl_global_messagequeue_pending(owl_global *g) |
---|
735 | { |
---|
736 | return !g_queue_is_empty(g->messagequeue); |
---|
737 | } |
---|
738 | |
---|
739 | owl_buddylist *owl_global_get_buddylist(owl_global *g) |
---|
740 | { |
---|
741 | return(&(g->buddylist)); |
---|
742 | } |
---|
743 | |
---|
744 | /* style */ |
---|
745 | |
---|
746 | /* Return the style with name 'name'. If it does not exist return |
---|
747 | * NULL */ |
---|
748 | const owl_style *owl_global_get_style_by_name(const owl_global *g, const char *name) |
---|
749 | { |
---|
750 | return owl_dict_find_element(&(g->styledict), name); |
---|
751 | } |
---|
752 | |
---|
753 | CALLER_OWN GPtrArray *owl_global_get_style_names(const owl_global *g) |
---|
754 | { |
---|
755 | return owl_dict_get_keys(&g->styledict); |
---|
756 | } |
---|
757 | |
---|
758 | void owl_global_add_style(owl_global *g, owl_style *s) |
---|
759 | { |
---|
760 | /* |
---|
761 | * If we're redefining the current style, make sure to update |
---|
762 | * pointers to it. |
---|
763 | */ |
---|
764 | if(g->current_view.style |
---|
765 | && !strcmp(owl_style_get_name(g->current_view.style), |
---|
766 | owl_style_get_name(s))) |
---|
767 | g->current_view.style = s; |
---|
768 | owl_dict_insert_element(&(g->styledict), owl_style_get_name(s), |
---|
769 | s, (void (*)(void *))owl_style_delete); |
---|
770 | } |
---|
771 | |
---|
772 | void owl_global_set_haveaim(owl_global *g) |
---|
773 | { |
---|
774 | g->haveaim=1; |
---|
775 | } |
---|
776 | |
---|
777 | int owl_global_is_haveaim(const owl_global *g) |
---|
778 | { |
---|
779 | if (g->haveaim) return(1); |
---|
780 | return(0); |
---|
781 | } |
---|
782 | |
---|
783 | void owl_global_set_ignore_aimlogin(owl_global *g) |
---|
784 | { |
---|
785 | g->ignoreaimlogin = 1; |
---|
786 | } |
---|
787 | |
---|
788 | void owl_global_unset_ignore_aimlogin(owl_global *g) |
---|
789 | { |
---|
790 | g->ignoreaimlogin = 0; |
---|
791 | } |
---|
792 | |
---|
793 | int owl_global_is_ignore_aimlogin(const owl_global *g) |
---|
794 | { |
---|
795 | return g->ignoreaimlogin; |
---|
796 | } |
---|
797 | |
---|
798 | void owl_global_set_havezephyr(owl_global *g) |
---|
799 | { |
---|
800 | g->havezephyr=1; |
---|
801 | } |
---|
802 | |
---|
803 | int owl_global_is_havezephyr(const owl_global *g) |
---|
804 | { |
---|
805 | if (g->havezephyr) return(1); |
---|
806 | return(0); |
---|
807 | } |
---|
808 | |
---|
809 | owl_errqueue *owl_global_get_errqueue(owl_global *g) |
---|
810 | { |
---|
811 | return(&(g->errqueue)); |
---|
812 | } |
---|
813 | |
---|
814 | owl_zbuddylist *owl_global_get_zephyr_buddylist(owl_global *g) |
---|
815 | { |
---|
816 | return(&(g->zbuddies)); |
---|
817 | } |
---|
818 | |
---|
819 | GList **owl_global_get_zaldlist(owl_global *g) |
---|
820 | { |
---|
821 | return &(g->zaldlist); |
---|
822 | } |
---|
823 | |
---|
824 | int owl_global_get_pseudologin_notify(owl_global *g) |
---|
825 | { |
---|
826 | return g->pseudologin_notify; |
---|
827 | } |
---|
828 | |
---|
829 | void owl_global_set_pseudologin_notify(owl_global *g, int notify) |
---|
830 | { |
---|
831 | g->pseudologin_notify = notify; |
---|
832 | } |
---|
833 | |
---|
834 | struct termios *owl_global_get_startup_tio(owl_global *g) |
---|
835 | { |
---|
836 | return(&(g->startup_tio)); |
---|
837 | } |
---|
838 | |
---|
839 | void owl_global_setup_default_filters(owl_global *g) |
---|
840 | { |
---|
841 | int i; |
---|
842 | static const struct { |
---|
843 | const char *name; |
---|
844 | const char *desc; |
---|
845 | } filters[] = { |
---|
846 | { "personal", |
---|
847 | "isprivate ^true$ and ( not type ^zephyr$ or ( class ^message ) )" }, |
---|
848 | { "trash", |
---|
849 | "class ^mail$ or opcode ^ping$ or type ^admin$ or ( not login ^none$ )" }, |
---|
850 | { "wordwrap", "not ( type ^admin$ or type ^zephyr$ )" }, |
---|
851 | { "ping", "opcode ^ping$" }, |
---|
852 | { "auto", "opcode ^auto$" }, |
---|
853 | { "login", "not login ^none$" }, |
---|
854 | { "reply-lockout", "class ^mail$" }, |
---|
855 | { "out", "direction ^out$" }, |
---|
856 | { "aim", "type ^aim$" }, |
---|
857 | { "zephyr", "type ^zephyr$" }, |
---|
858 | { "none", "false" }, |
---|
859 | { "all", "true" }, |
---|
860 | { NULL, NULL } |
---|
861 | }; |
---|
862 | |
---|
863 | owl_function_debugmsg("startup: creating default filters"); |
---|
864 | |
---|
865 | for (i = 0; filters[i].name != NULL; i++) |
---|
866 | owl_global_add_filter(g, owl_filter_new_fromstring(filters[i].name, |
---|
867 | filters[i].desc)); |
---|
868 | } |
---|
869 | |
---|
870 | FILE *owl_global_get_debug_file_handle(owl_global *g) { |
---|
871 | static char *open_file = NULL; |
---|
872 | const char *filename = owl_global_get_debug_file(g); |
---|
873 | if (g->debug_file == NULL || |
---|
874 | (open_file && strcmp(filename, open_file) != 0)) { |
---|
875 | char *path; |
---|
876 | int fd; |
---|
877 | |
---|
878 | if (g->debug_file) |
---|
879 | fclose(g->debug_file); |
---|
880 | |
---|
881 | g->debug_file = NULL; |
---|
882 | |
---|
883 | path = g_strdup_printf("%s.%d", filename, getpid()); |
---|
884 | fd = open(path, O_CREAT|O_WRONLY|O_EXCL, 0600); |
---|
885 | g_free(path); |
---|
886 | |
---|
887 | if (fd >= 0) |
---|
888 | g->debug_file = fdopen(fd, "a"); |
---|
889 | |
---|
890 | g_free(open_file); |
---|
891 | open_file = g_strdup(filename); |
---|
892 | } |
---|
893 | return g->debug_file; |
---|
894 | } |
---|
895 | |
---|
896 | const char *owl_global_get_kill_buffer(owl_global *g) { |
---|
897 | return g->kill_buffer; |
---|
898 | } |
---|
899 | |
---|
900 | void owl_global_set_kill_buffer(owl_global *g, const char *kill, int len) { |
---|
901 | g_free(g->kill_buffer); |
---|
902 | g->kill_buffer = g_strndup(kill, len); |
---|
903 | } |
---|
904 | |
---|
905 | static GMutex *owl_global_get_interrupt_lock(owl_global *g) |
---|
906 | { |
---|
907 | #if GLIB_CHECK_VERSION(2, 31, 0) |
---|
908 | return &g->interrupt_lock; |
---|
909 | #else |
---|
910 | return g->interrupt_lock; |
---|
911 | #endif |
---|
912 | } |
---|
913 | |
---|
914 | void owl_global_add_interrupt(owl_global *g) { |
---|
915 | /* TODO: This can almost certainly be done with atomic |
---|
916 | * operations. Whatever. */ |
---|
917 | g_mutex_lock(owl_global_get_interrupt_lock(g)); |
---|
918 | g->interrupt_count++; |
---|
919 | g_mutex_unlock(owl_global_get_interrupt_lock(g)); |
---|
920 | } |
---|
921 | |
---|
922 | bool owl_global_take_interrupt(owl_global *g) { |
---|
923 | bool ans = false; |
---|
924 | g_mutex_lock(owl_global_get_interrupt_lock(g)); |
---|
925 | if (g->interrupt_count > 0) { |
---|
926 | ans = true; |
---|
927 | g->interrupt_count--; |
---|
928 | } |
---|
929 | g_mutex_unlock(owl_global_get_interrupt_lock(g)); |
---|
930 | return ans; |
---|
931 | } |
---|