summaryrefslogtreecommitdiffstats
path: root/src/sendphonebook.cs
blob: cc1dc62e1d1be4d7d84da56e7044560b52cd2cbd (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
//  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 GLib;
	using Gtk;
	using Gnome;
	using Glade;
	using GtkSharp;
	using System.Runtime.InteropServices;
	using System.Collections;
	using System.Reflection;

	//************************************************************************
	// GfaxSendPhoneBook class
	//
	// 
	// 
	public class GfaxSendPhoneBook
	{
		Gtk.Dialog phbd;
		Gtk.TreeView book, list;
		//Gtk.TreeModel book_model; delete me
		Gtk.ListStore book_store, list_store;
		G_ListView bs, ls;
		Glade.XML gsxml;
		
		[Glade.Widget] Gtk.Dialog PhbookDialog;
		[Glade.Widget] Gtk.TreeView book_treeview;
		[Glade.Widget] Gtk.TreeView list_treeview;
		
		const int COLUMN_0 = 0;
		const int COLUMN_1 = 1;
		const int COLUMN_2 = 2;
		const int ALL_COLUMNS = -1;	
		//string parent; delete me
		Phonebook[] myPhoneBooks;
		
		public GfaxSendPhoneBook (Glade.XML xml, string myparent)
		{
			
			//Phonebook[] pb; delete me
			
			//gxml = xml;
			//parent = myparent; delete me
			myPhoneBooks = Phonetools.get_phonebooks();
			
			if ( myPhoneBooks == null ) {
				G_Message m = new G_Message(Catalog.GetString("You don't have any phone books yet."));
				m = null;
				return;
			}
		
			gsxml = new Glade.XML (null, "send-druid.glade","PhbookDialog",null);
			gsxml.Autoconnect (this);
			
			// Get the widget so we can manipulate it
			//phbd = (Gtk.Dialog) gsxml.GetWidget("PhbookDialog");
			//book = (Gtk.TreeView) gsxml.GetWidget("book_treeview");
			//list = (Gtk.TreeView) gsxml.GetWidget("list_treeview");
			phbd = PhbookDialog;
			book = book_treeview;
			list = list_treeview;
			
			book.Selection.Changed += new EventHandler (on_book_treeview_selection);
			
			phbd.Resizable = true;
			book_store = new ListStore(typeof(string));
			book.HeadersVisible = false;
			book.Selection.Mode = SelectionMode.Multiple;

			bs = new G_ListView(book, book_store);
			// Got have a column title or things won't show up
			bs.AddColumnTitle(Catalog.GetString("Phone books"), 0, COLUMN_0);
			
			
			list_store = new ListStore(
					typeof (string),
					typeof (string),
					typeof (string));
			
			ls = new G_ListView(list, list_store);
			ls.AddColumnTitle(Catalog.GetString("Organization"), 0, COLUMN_0);
			ls.AddColumnTitle(Catalog.GetString("Phone Number"), 1, COLUMN_1);
			ls.AddColumnTitle(Catalog.GetString("Contact"), 2, COLUMN_2);
			list.HeadersVisible = true;
			list.Selection.Mode = SelectionMode.Multiple;
			
			// populate the list
			foreach (Phonebook p in myPhoneBooks)
				bs.AddTextToRow(p.Name);

			phbd.Run();
		}
		
		// load the phone book
		// Since we have SelectionMode.Multiple turned on we have to 
		// jump through these hoops in GTK-2.0 to get a selection
		private void on_book_treeview_selection(object o, EventArgs args)
		{	
			Gtk.TreeIter iter = new Gtk.TreeIter();
            //Value value = new Value(); delete me
            string selectionText = null;
			
			book_store.GetIterFirst(out iter);
			if ( book.Selection.IterIsSelected(iter)) {
				selectionText = (string)book_store.GetValue(iter, 0);
			}
			
			while (book_store.IterNext(ref iter)) {
				if ( book.Selection.IterIsSelected(iter)) {
					selectionText = (string)book_store.GetValue(iter, 0);
				}
			}
			
			// Ok now we can finally load the phone book
			foreach (Phonebook p in myPhoneBooks)
				if (p.Name == selectionText)
					load_phone_book(p);
	
		}

		// If we double click the phonebook.
		private void on_book_treeview_row_activated(object o, RowActivatedArgs args)
		{	
			/*
			ArrayList bsdest = new ArrayList();
			ArrayList contacts = new ArrayList();
						
			bsdest = bs.GetSelections(COLUMN_0);
			if ( bsdest.Count > 0 ) {
				IEnumerator enu = bsdest.GetEnumerator();
				while ( enu.MoveNext() ) {
					foreach (Phonebook p in myPhoneBooks)
					if (p.Name == (string)enu.Current)
						contacts = Phonetools.get_contacts(p);
					
					// add contacts to global desinations
					IEnumerator enuc = contacts.GetEnumerator();
					while ( enuc.MoveNext() )
						gfax.Destinations.Add((Contact)enuc.Current);
				}
			}
			
			phbd.Destroy();
		*/
		}


		private void on_list_treeview_row_activated(object o, RowActivatedArgs args)
		{
		}

		private void on_ok_button_clicked(object o, EventArgs args)
		{
			ArrayList lsdest = new ArrayList();
			ArrayList bsdest = new ArrayList();
			ArrayList contacts = new ArrayList();
			
			lsdest = ls.GetSelections(ALL_COLUMNS);
			
			// if there are indiviual entries don't do entire phonebooks
			if (lsdest.Count > 0) {
				IEnumerator enu = lsdest.GetEnumerator();
				while ( enu.MoveNext() ) {
					GfaxContact c = new GfaxContact();
					c.Organization = (string)enu.Current;
					enu.MoveNext();
					c.PhoneNumber = (string)enu.Current;
					enu.MoveNext();
					c.ContactPerson = (string)enu.Current;
					gfax.Destinations.Add(c);
				}
			} 
			else {	
				bsdest = bs.GetSelections(COLUMN_0);
				if ( bsdest.Count > 0 ) {
					IEnumerator enu = bsdest.GetEnumerator();
					while ( enu.MoveNext() ) {
						foreach (Phonebook p in myPhoneBooks)
						if (p.Name == (string)enu.Current)
							contacts = Phonetools.get_contacts(p);

						// add contacts to global desinations
						if (contacts.Count > 0) {
							IEnumerator enuc = contacts.GetEnumerator();
							while ( enuc.MoveNext() )
								gfax.Destinations.Add((GfaxContact)enuc.Current);
						}
					}
				}
			}
				
			phbd.Destroy();
		}

		// loads the phone book into list_store
		private void load_phone_book(Phonebook p)
		{
			ArrayList contacts = null;
						
			// Clear the list_store
			list_store.Clear();
			
			contacts = Phonetools.get_contacts(p);
			if (contacts == null)
				return;
				
			IEnumerator enu = contacts.GetEnumerator();
			while ( enu.MoveNext() ) {
				GfaxContact c = new GfaxContact();
				c = (GfaxContact)enu.Current;
				ls.AddTextToRow(c.Organization, c.PhoneNumber, c.ContactPerson);
			}
		}

		private void on_cancel_button_clicked(object o, EventArgs args)
		{
			phbd.Destroy();
		}

	}
	
}