summaryrefslogtreecommitdiff
path: root/MainForm.cs
blob: ab85f972cc7c8a080b6fb1a05476d856fcdcdb19 (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
387
388
389
390
391
392
// TLReader - reads access database containing tug info
// Copyright (C) 2004  Ian Cowburn
// 
// 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 2
// 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 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.
//
using System;
using System.Windows.Forms;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Collections;
using System.Collections.Specialized;
using System.IO;

namespace TLReader
{
	/// <summary>
	/// Description of MainForm.	
	/// </summary>
	public class MainForm : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Button m_quitButton;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.CheckBox m_sortOrder;
		private System.Windows.Forms.ProgressBar m_progress;
		private System.Windows.Forms.Label label;
		private System.Windows.Forms.ComboBox m_sortBy;
		private System.Windows.Forms.Button m_csvButton;
		private System.Windows.Forms.ListView m_list;
		private System.Windows.Forms.TextBox m_noRowsText;
		private System.Windows.Forms.Button m_aboutButton;
		private System.Windows.Forms.Button m_sortButton;
		
		private Query				m_query;
		private TLConfig			m_config;
		private StringCollection	m_colDesc;
		private StringCollection	m_colName;

		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			m_config=Config.Load();
			
			Text+=" - "+m_config.Connect;
			
			m_colDesc=Util.Split(m_config.DisplayNames);
			m_colName=Util.Split(m_config.ColumnNames);
			
			m_sortBy.Items.Clear();
			
			foreach (string s in m_colDesc)
			{
				m_sortBy.Items.Add(s);
			}
			
			m_sortBy.SelectedIndex=0;
			
			DoQuery();
		}
		
		[STAThread]
		public static void Main(string[] args)
		{
			try
			{
				Application.Run(new MainForm());
			}
			catch (OdbcException e)
			{
				string error="";
				
				for (int i=0; i < e.Errors.Count; i++)
				{
					error += "Index #" + i + "\n" +
				             "Message: " + e.Errors[i].Message + "\n" +
				             "NativeError: " + e.Errors[i].NativeError.ToString() + "\n" +
				             "Source: " + e.Errors[i].Source + "\n" +
				             "SQL: " + e.Errors[i].SQLState + "\n";
				}
				
				Util.Error(error);
			}
			catch(Exception e)
			{
				Util.Error(e.ToString());
			}
		}
		
		#region Windows Forms Designer generated code
		/// <summary>
		/// This method is required for Windows Forms designer support.
		/// Do not change the method contents inside the source code editor. The Forms designer might
		/// not be able to load this method if it was changed manually.
		/// </summary>
		private void InitializeComponent() {
			this.m_sortButton = new System.Windows.Forms.Button();
			this.m_aboutButton = new System.Windows.Forms.Button();
			this.m_noRowsText = new System.Windows.Forms.TextBox();
			this.m_list = new System.Windows.Forms.ListView();
			this.m_csvButton = new System.Windows.Forms.Button();
			this.m_sortBy = new System.Windows.Forms.ComboBox();
			this.label = new System.Windows.Forms.Label();
			this.m_progress = new System.Windows.Forms.ProgressBar();
			this.m_sortOrder = new System.Windows.Forms.CheckBox();
			this.label2 = new System.Windows.Forms.Label();
			this.m_quitButton = new System.Windows.Forms.Button();
			this.SuspendLayout();
			// 
			// m_sortButton
			// 
			this.m_sortButton.Location = new System.Drawing.Point(568, 8);
			this.m_sortButton.Name = "m_sortButton";
			this.m_sortButton.Size = new System.Drawing.Size(104, 32);
			this.m_sortButton.TabIndex = 9;
			this.m_sortButton.Text = "Sort";
			this.m_sortButton.Click += new System.EventHandler(this.OnSort);
			// 
			// m_aboutButton
			// 
			this.m_aboutButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
			this.m_aboutButton.Location = new System.Drawing.Point(8, 504);
			this.m_aboutButton.Name = "m_aboutButton";
			this.m_aboutButton.Size = new System.Drawing.Size(104, 32);
			this.m_aboutButton.TabIndex = 4;
			this.m_aboutButton.Text = "About";
			this.m_aboutButton.Click += new System.EventHandler(this.OnAbout);
			// 
			// m_noRowsText
			// 
			this.m_noRowsText.Location = new System.Drawing.Point(104, 8);
			this.m_noRowsText.Name = "m_noRowsText";
			this.m_noRowsText.ReadOnly = true;
			this.m_noRowsText.Size = new System.Drawing.Size(72, 21);
			this.m_noRowsText.TabIndex = 6;
			this.m_noRowsText.TabStop = false;
			this.m_noRowsText.Text = "textBox";
			// 
			// m_list
			// 
			this.m_list.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
						| System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.m_list.BackColor = System.Drawing.SystemColors.Info;
			this.m_list.FullRowSelect = true;
			this.m_list.GridLines = true;
			this.m_list.HideSelection = false;
			this.m_list.Location = new System.Drawing.Point(8, 48);
			this.m_list.MultiSelect = false;
			this.m_list.Name = "m_list";
			this.m_list.Size = new System.Drawing.Size(664, 440);
			this.m_list.TabIndex = 2;
			this.m_list.View = System.Windows.Forms.View.Details;
			// 
			// m_csvButton
			// 
			this.m_csvButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.m_csvButton.Location = new System.Drawing.Point(448, 504);
			this.m_csvButton.Name = "m_csvButton";
			this.m_csvButton.Size = new System.Drawing.Size(104, 32);
			this.m_csvButton.TabIndex = 1;
			this.m_csvButton.Text = "Export as CSV";
			this.m_csvButton.Click += new System.EventHandler(this.OnExportCSV);
			// 
			// m_sortBy
			// 
			this.m_sortBy.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
			this.m_sortBy.Location = new System.Drawing.Point(256, 8);
			this.m_sortBy.Name = "m_sortBy";
			this.m_sortBy.Size = new System.Drawing.Size(208, 21);
			this.m_sortBy.TabIndex = 8;
			// 
			// label
			// 
			this.label.Location = new System.Drawing.Point(8, 8);
			this.label.Name = "label";
			this.label.Size = new System.Drawing.Size(88, 24);
			this.label.TabIndex = 5;
			this.label.Text = "Number of Rows";
			this.label.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// m_progress
			// 
			this.m_progress.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
						| System.Windows.Forms.AnchorStyles.Right)));
			this.m_progress.Location = new System.Drawing.Point(128, 512);
			this.m_progress.Name = "m_progress";
			this.m_progress.Size = new System.Drawing.Size(304, 16);
			this.m_progress.TabIndex = 3;
			// 
			// m_sortOrder
			// 
			this.m_sortOrder.Checked = true;
			this.m_sortOrder.CheckState = System.Windows.Forms.CheckState.Checked;
			this.m_sortOrder.Location = new System.Drawing.Point(480, 8);
			this.m_sortOrder.Name = "m_sortOrder";
			this.m_sortOrder.Size = new System.Drawing.Size(80, 24);
			this.m_sortOrder.TabIndex = 10;
			this.m_sortOrder.Text = "Ascending";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(200, 8);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(56, 24);
			this.label2.TabIndex = 7;
			this.label2.Text = "Sort By";
			this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
			// 
			// m_quitButton
			// 
			this.m_quitButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
			this.m_quitButton.Location = new System.Drawing.Point(568, 504);
			this.m_quitButton.Name = "m_quitButton";
			this.m_quitButton.Size = new System.Drawing.Size(104, 32);
			this.m_quitButton.TabIndex = 0;
			this.m_quitButton.Text = "Quit";
			this.m_quitButton.Click += new System.EventHandler(this.OnQuit);
			// 
			// MainForm
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.ClientSize = new System.Drawing.Size(680, 541);
			this.Controls.Add(this.m_sortOrder);
			this.Controls.Add(this.m_sortButton);
			this.Controls.Add(this.m_sortBy);
			this.Controls.Add(this.label2);
			this.Controls.Add(this.m_noRowsText);
			this.Controls.Add(this.label);
			this.Controls.Add(this.m_aboutButton);
			this.Controls.Add(this.m_progress);
			this.Controls.Add(this.m_list);
			this.Controls.Add(this.m_csvButton);
			this.Controls.Add(this.m_quitButton);
			this.MinimumSize = new System.Drawing.Size(688, 568);
			this.Name = "MainForm";
			this.Text = "Tuglist Reader";
			this.ResumeLayout(false);
		}
		#endregion

		void OnQuit(object sender, System.EventArgs e)
		{
			Close();
		}
		
		void OnExportCSV(object sender, System.EventArgs e)
		{
			SaveFileDialog fsel=new SaveFileDialog();
			
			fsel.Filter="CSV files (*.csv)|*.csv|All files (*.*)|*.*";
			fsel.RestoreDirectory=true;
			
			if(fsel.ShowDialog()==DialogResult.OK)
			{
				StreamWriter str=File.CreateText(fsel.FileName);
				
				if(str!=null)
				{
					m_progress.Maximum=m_list.Items.Count+1;
					m_progress.Step=1;
					m_progress.Value=0;
			
					CSV csv=new CSV(str);
					
					foreach (string s in m_query.Fields)
					{
						csv.Value(s);
					}
					
					csv.EndLine();
					m_progress.PerformStep();

					foreach (ListViewItem i in m_list.Items)
					{
						//csv.Value(i.Text);
						
						foreach (ListViewItem.ListViewSubItem si in i.SubItems)
						{
							csv.Value(si.Text);
						}
						
						csv.EndLine();
						m_progress.PerformStep();
					}
			
					str.Close();
				}
				else
				{
					Util.Error("Failed to open "+fsel.FileName);
				}
			}

			m_progress.Value=0;
		}
		
		private void DoQuery()
		{
			Enabled=false;
			Cursor=Cursors.WaitCursor;
			
			string sort=m_colName[m_sortBy.SelectedIndex];
			string order=m_sortOrder.Checked ? "asc":"desc";
			
			OdbcConnection dbase=new OdbcConnection(m_config.Connect);

			dbase.Open();
			
			m_query=new Query(dbase,
			                  m_config.DisplayNames,
			                  m_config.Query+" order by "+sort+" "+order);

			dbase.Close();
			
			FillList();
			
			m_noRowsText.Text=m_query.Count.ToString();
			
			Cursor=Cursors.Default;
			Enabled=true;
		}
		
		private void FillList()
		{
			int f;
			
			m_list.BeginUpdate();
			
			m_list.Columns.Clear();
			m_list.Items.Clear();
			
			foreach (string s in m_query.Fields)
			{
				m_list.Columns.Add(s,100,HorizontalAlignment.Left);
			}
			
			for(f=0;f<m_query.Count;f++)
			{
				ArrayList l=new ArrayList();
				
				foreach (string s in m_query.Row(f))
				{
					l.Add(s);
				}
				
				AddRow(l);
			}
			
			m_list.EndUpdate();
		}
		
		private void AddRow(ArrayList l)
		{
			ListViewItem i=new ListViewItem((string)l[0]);
			
			for(int f=1;f<l.Count;f++)
			{
				i.SubItems.Add((string)l[f]);
			}
			
			m_list.Items.Add(i);
		}

		void OnAbout(object sender, System.EventArgs e)
		{
			About a=new About();
			
			a.ShowDialog(this);
		}
		
		void OnSort(object sender, System.EventArgs e)
		{
			DoQuery();
		}
	}
}