Commit 29702c75 29702c75817286cf4f8fae5d49bf020e15638be3 by Sergey Poznyakoff

New file. Global configuration functions

1 parent 11bb126a
1 /* GNU mailutils - a suite of utilities for electronic mail
2 Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
17
18 /* Global MH state. */
19
20 #include <mh.h>
21
22 char *current_folder = NULL;
23 size_t current_message = 0;
24 mh_context_t *context;
25 mh_context_t *profile;
26 mh_context_t *sequences;
27
28 /* Global profile */
29
30 char *
31 mh_global_profile_get (char *name, char *defval)
32 {
33 return mh_context_get_value (profile, name, defval);
34 }
35
36 int
37 mh_global_profile_set (const char *name, const char *value)
38 {
39 return mh_context_set_value (profile, name, value);
40 }
41
42 void
43 mh_read_profile ()
44 {
45 char *p;
46
47 p = getenv ("MH");
48 if (p)
49 p = mu_tilde_expansion (p, "/", NULL);
50 else
51 {
52 char *home = mu_get_homedir ();
53 if (!home)
54 abort (); /* shouldn't happen */
55 asprintf (&p, "%s/%s", home, MH_USER_PROFILE);
56 free (home);
57 }
58 profile = mh_context_create (p, 1);
59 mh_context_read (profile);
60 }
61
62 /* Global context */
63
64 void
65 _mh_init_global_context ()
66 {
67 char *p, *ctx_name;
68
69 if (context)
70 return;
71 mu_path_folder_dir = mh_get_dir ();
72 p = getenv ("CONTEXT");
73 if (!p)
74 p = "context";
75 ctx_name = mh_expand_name (p, 0);
76 context = mh_context_create (ctx_name, 1);
77 mh_context_read (context);
78
79 if (!current_folder)
80 current_folder = mh_context_get_value (context, "Current-Folder",
81 mh_global_profile_get ("Inbox",
82 "inbox"));
83 }
84
85 char *
86 mh_global_context_get (const char *name, const char *defval)
87 {
88 _mh_init_global_context ();
89 return mh_context_get_value (context, name, defval);
90 }
91
92 int
93 mh_global_context_set (const char *name, const char *value)
94 {
95 _mh_init_global_context ();
96 return mh_context_set_value (context, name, value);
97 }
98
99 char *
100 mh_current_folder ()
101 {
102 return mh_global_context_get ("Current-Folder",
103 mh_global_profile_get ("Inbox", "inbox"));
104 }
105
106 /* Global sequences */
107
108 void
109 _mh_init_global_sequences ()
110 {
111 char *name, *p, *seq_name;
112
113 if (sequences)
114 return;
115
116 _mh_init_global_context ();
117 name = mh_global_profile_get ("mh-sequences", MH_SEQUENCES_FILE);
118 p = mh_expand_name (current_folder, 0);
119 asprintf (&seq_name, "%s/%s", p, name);
120 free (p);
121 sequences = mh_context_create (seq_name, 1);
122 if (mh_context_read (sequences) == 0)
123 {
124 p = mh_context_get_value (sequences, "cur", "0");
125 current_message = strtoul (p, NULL, 10);
126 }
127 }
128
129 char *
130 mh_global_sequences_get (const char *name, const char *defval)
131 {
132 _mh_init_global_sequences ();
133 return mh_context_get_value (sequences, name, defval);
134 }
135
136 int
137 mh_global_sequences_set (const char *name, const char *value)
138 {
139 _mh_init_global_sequences ();
140 return mh_context_set_value (sequences, name, value);
141 }
142
143 /* Global state */
144
145 void
146 mh_global_save_state ()
147 {
148 char buf[64];
149 snprintf (buf, sizeof buf, "%d", current_message);
150 mh_context_set_value (sequences, "cur", buf);
151 mh_context_write (sequences);
152
153 mh_context_set_value (context, "Current-Folder", current_folder);
154 mh_context_write (context);
155 }