summaryrefslogtreecommitdiffstats
path: root/src/phonebook.cs
blob: fbb86218e2c075cd65408881d21bb8d0ac6fc390 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
//  GFAX - Gnome fax application
//  Copyright (C) 2003 - 2008 George A. Farris
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Library General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

namespace gfax {
	using Mono.Unix;
	using System;
	using System.IO;
	using System.Text;
	using System.Collections;
	
	// Phone book that holds multiple numbers
	public class Phonebook
	{
		public string Name;	//Name book is called by
		public string Path;	//Filename including path
		public string Type; // Type of phone book (gfax,gcard etc)
	}
	
	// Individual contact in phone book
	public class GfaxContact
	{
		public string Organization;
		public string PhoneNumber;
		public string ContactPerson;
	}
	
	public class Phonetools
	{
		public static Phonebook[] get_phonebooks ()
		{
			StreamReader infile = null;
			string buf;
			bool migrate = false;
			
			Phonebook[] pbooks = null;
						
			// TODO  get location from gconf
			string HOMEDIR = Environment.GetEnvironmentVariable("HOME");
			string PHONEBOOKS = gfax.ConfigDirectory + "/phonebooks";
			
			#if DEBUG
				Console.WriteLine("[Phonebook] Reading phonebook file");
			#endif
			
			int numberOfBooks = get_number_of_books();
			pbooks = new Phonebook[numberOfBooks];			
			
			if (numberOfBooks == 0) {
				return (pbooks);
			}
			
				
			try { 
				infile = File.OpenText(PHONEBOOKS); 
				int i = 0;
				
				while ( (buf = infile.ReadLine()) != null ) {
					switch (buf.Trim()) {
						case "<book>" :
							// TODO more robust file reading
							Phonebook c = new Phonebook();
							c.Name = strip_tag(infile.ReadLine(), "name");
							c.Type = strip_tag(infile.ReadLine(), "type");
							c.Path = strip_tag(infile.ReadLine(), "path");
							
							// Migrate from old location
							if (c.Type == "gfax") {
								if (Path.GetDirectoryName(c.Path) == HOMEDIR + "/.etc/gfax") {
									c.Path = gfax.ConfigDirectory + "/" + Path.GetFileName(c.Path);
									migrate = true;
								}
							}
							
							pbooks[i++] = c;
							continue;
					}
				}

				infile.Close();
				if (migrate) 
					save_phonebooks(pbooks);
				return pbooks;			
			}
			catch (Exception e) { 
				Console.WriteLine("get_phonebooks - Exception in phonebook.cs {0}", e);
				return pbooks; 
			}

		}
		
		// save or create phonebooks files
		public static void save_phonebooks (Phonebook[] pbooks)
		{
			StreamWriter outfile;
									
			// TODO  get location from gconf
			string PHONEBOOKS = gfax.ConfigDirectory + "/phonebooks";
			
			try { 
				outfile = File.CreateText(PHONEBOOKS); 
			} catch (Exception e) { 
				Console.WriteLine("Exception in phonebook.cs {0}", e);
				return; 
			}

			outfile.WriteLine("<gfax>");
			Console.WriteLine("Len :{0}", pbooks.Length);
			foreach (Phonebook p in pbooks) {
				outfile.WriteLine("	<book>");
				outfile.WriteLine("		<name>" + p.Name + "</name>");
				outfile.WriteLine("		<type>" + p.Type + "</type>");
				outfile.WriteLine("		<path>" + p.Path + "</path>");
				outfile.WriteLine("	</book>");
			}
			outfile.WriteLine("</gfax>");
			outfile.Close();
		}
		
		public static void delete_book (string book)
		{
			StreamReader infile = null;
			string buf;
			string deleteme = null;
			Phonebook[] pbooks = null;
			
			// make array size less 1 because we're deleting 1
			pbooks = new Phonebook[get_number_of_books() - 1];
			
			// TODO  get location from gconf
			string PHONEBOOKS = gfax.ConfigDirectory + "/phonebooks";
			string type = "";
			try { 
				infile = File.OpenText(PHONEBOOKS); 
				int i = 0;
			
				// iterate through the phonebook file and skip past book to delete
				while ( (buf = infile.ReadLine()) != null ) {
					switch (buf.Trim()) {
						case "<book>" :
							// TODO more robust file reading
							Phonebook c = new Phonebook();
							c.Name = strip_tag(infile.ReadLine(), "name");
							c.Type = strip_tag(infile.ReadLine(), "type");
							c.Path = strip_tag(infile.ReadLine(), "path");
						
							if (c.Name != book)
								pbooks[i++] = c;
							else {
								deleteme = c.Path;
								type = "gfax";
							}
							continue;
					}
				}
				infile.Close();
				save_phonebooks(pbooks);
				
				if (type == "gfax")
					if (File.Exists(deleteme))
						File.Delete(deleteme);
			}
			catch (Exception e) {
				Console.WriteLine("delete_book - Exception in phonebook.cs {0}", e);
				return; 
			}
		}
		
		// Create or add the new phone book to the "phonebooks" file.
		public static void add_book (Phonebook p)
		{
			StreamReader infile = null;
			string buf;
			Phonebook[] pbooks = null;
			
			// make array plus 1 because we're adding 1
			pbooks = new Phonebook[get_number_of_books() + 1];
			
			// TODO  get location from gconf
			string PHONEBOOKS = gfax.ConfigDirectory + "/phonebooks";
			
			if (p.Type == "gfax") {
				// add default path if not specified
				if (Path.GetDirectoryName(p.Path) == "")
					p.Path = gfax.ConfigDirectory + p.Path;
			}
				// Create the file.
			if (!File.Exists(PHONEBOOKS)) {
				FileStream fs = File.Create(PHONEBOOKS); 
				fs.Close();
			}

			int i = 0;
			try { 
				infile = File.OpenText(PHONEBOOKS); 
				
				while ( (buf = infile.ReadLine()) != null ) {
					switch (buf.Trim()) {
						case "<book>" :
							// TODO more robust file reading
							Phonebook c = new Phonebook();
							c.Name = strip_tag(infile.ReadLine(), "name");
							c.Type = strip_tag(infile.ReadLine(), "type");
							c.Path = strip_tag(infile.ReadLine(), "path");
										
							pbooks[i++] = c;
							continue;
					}
				}
				infile.Close();
			}
			catch (Exception e) { 
				// TODO catch file ops error
				Console.WriteLine("add_book - Exception in phonebook.cs {0}", e);
				return;
			}

			
			pbooks[i++] = p;	// add the new book
			save_phonebooks(pbooks);
		}

		// save a list (ArrayList) of contacts
		public static void save_phonebook_items (string book, ArrayList contacts)
		{
			Phonebook p;
			StreamWriter outfile;
						
			p = get_book_from_name(book);
			
			if (p.Type == "gfax") {
			// TODO error reporting
				try { outfile = File.CreateText(p.Path); }
				catch (Exception e) {
					Console.WriteLine("save_phonebook_items - Exception in phonebook.cs {0}", e);
					return; 
				}
			
				outfile.WriteLine("#Gfax phone book");
			
				IEnumerator enu = contacts.GetEnumerator();
				while ( enu.MoveNext() ) {
					GfaxContact c = new GfaxContact();
					c = (GfaxContact)enu.Current;
					outfile.WriteLine("{0}:{1}:{2}",c.PhoneNumber,c.ContactPerson,c.Organization);
				}
				outfile.Close();
			}
		}
		
		public static StreamReader open_phonebook (Phonebook p)
		{
			StreamReader infile = null;
			
			// If it doesn't exist yet just return
			if (!File.Exists(p.Path))
				return(null);
				
			try { 
				infile = File.OpenText(p.Path);
			}
			catch (Exception e) { 
				Console.WriteLine("open_phonebook - Exception in phonebook.cs {0}", e);
				return null;
			}
			return infile;
		}
		
		public static ArrayList get_contacts (Phonebook p)
		{
			string buf = null;
			string[] sa;
			//char[] ca = {':',':',':'}; delete me
			
			ArrayList records = new ArrayList();			
			StreamReader fp = null;

			// TODO add popup message
			if ( p.Type == "gfax" ) {
				fp = open_phonebook(p);
				if (fp == null) {
					Console.WriteLine(Catalog.GetString("Can't open file : {0}"), p.Path);
					return records;
				}

				while ( (buf = fp.ReadLine()) != null ) {
					buf.Trim();
				
					if (buf[0] == '#')
						continue;
					else {
						sa = buf.Split(':');
						GfaxContact contact = new GfaxContact();
						contact.PhoneNumber = sa[0];
						contact.ContactPerson = sa[1];
						contact.Organization = sa[2];
						records.Add(contact);
					}
				}
					
				fp.Close();
			}
			
			if ( p.Type == "evolution" ) {
				EdsPhoneBooks eds = new EdsPhoneBooks();
				ArrayList ebooks = new ArrayList();
				ebooks = eds.GetPhoneBooks();
				
				IEnumerator enu = ebooks.GetEnumerator();
				while ( enu.MoveNext() ) {
					if ((string)enu.Current == p.Name) {
						records = eds.GetContacts((string)enu.Current);
					}
				}
			}
			
			return records;
			
		}
		
		public static string strip_tag (string line, string tag)
		{
			string bt = "<" + tag + ">";
			string et = "</" + tag + ">";
			string s = (line.Trim()).Replace(bt, "");
			return s.Replace(et, "");
		}
		
		private static int get_number_of_books ()
		{
			StreamReader infile = null;
			string buf;
			int count = 0;
			
			// TODO  get location from gconf
			string PHONEBOOKS = gfax.ConfigDirectory + "/phonebooks";
			
			if (!File.Exists(PHONEBOOKS))
				return(0);
				
			try { 
				infile = File.OpenText(PHONEBOOKS); 
			} catch (Exception e) { 
				Console.WriteLine("get_number_of_books - Exception in phonebook.cs {0}", e);
				return 0; 
			}
			
			// how many phone books do we have, Trim() removes white space
			while ( (buf = infile.ReadLine()) != null ) {
				if ( buf.Trim() == "<book>") {
					count++;
				}
			}
			infile.Close();
			return count;
		}
		
		public static Phonebook get_book_from_name(string name)
		{
			Phonebook[] books;
			//int count; delete me
			
			//count = get_number_of_books(); delete me
			books = get_phonebooks();
			
			foreach (Phonebook p in books) {
				if (name == p.Name)
					return p;
			}
			return null;
		}
	}
}