summaryrefslogtreecommitdiffstats
path: root/src/main.cs
blob: e44b897b893ce6da74787d343f051c770d6b392c (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
//  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 GLib;
	using Gtk;
	using GtkSharp;
	using Mono.Unix;
	using System;
	using System.IO;
	using System.Collections;
	using System.Text;			//Encoding.ASCII.GetBytes
	using System.Threading;
	using System.Diagnostics;


	
	public class gfax
	{	
		// Gfax Global variables ( ya, ya I know)
		//
		// list of contacts of type Contact to xmit, we use this everywhere so it is
		// simpler to make it public and global.
		public static ArrayList Destinations = new ArrayList();
		public static DateTime timeToSend;
		public static bool quitAfterSend = false;
		public static bool fromSendWizard = false;
		public static bool sendWizardResolution = false;
		public static bool sendWizardEmailNotify = false;
		public static string sendWizardEmailAddress = null;
		public static string hylafaxPassword = null;
		public static string ConfigDirectory = null;
		public static string SpoolDirectory = null;
		public static bool activeNetwork = false;
		public static Gnome.AppBar GAppbar = null;
		public static Gtk.ProgressBar MainProgressBar = null;
		public static Gtk.TextBuffer GStatusTextBuffer = null;
		public static Gtk.Window MainWindow = null;
		public static string asyncFolderMessageBuffer = "";
		public static string asyncServerMessageBuffer = "";
		public static string asyncReadType = "";
		
		public static Efax efax;
		public static Status Status;
		public static Pulser Pulser;
		
		public static string filename = null;
		public static string[] pargs;
		
		const bool TOPLEVEL = true;
				
				
		public static void Main (string[] args)
		{
		
			string HOMEDIR = Environment.GetEnvironmentVariable("HOME");
			string USER = Environment.GetEnvironmentVariable("USER");
			ConfigDirectory = HOMEDIR + "/.config/gfax";
			SpoolDirectory = HOMEDIR + "/.local/share/gfax/spool";
			pargs = args;
			
			// TODO put this is try/catch
			if ( !Directory.Exists(ConfigDirectory)) {
				if ( !Directory.Exists(HOMEDIR + "/.config")) {
					Directory.CreateDirectory(HOMEDIR + "/.config");
				}
				// Move data from old ~/.etc directory
				if ( Directory.Exists(HOMEDIR + "/.etc/gfax")) {
					Directory.Move(HOMEDIR + "/.etc/gfax", ConfigDirectory);
				} else {
					Directory.CreateDirectory(ConfigDirectory);
				}
			}
			if ( !Directory.Exists(HOMEDIR + "/.local/share/gfax/spool") ) {
				if ( !Directory.Exists(HOMEDIR + "/.local/share/gfax") ) {
					if ( !Directory.Exists(HOMEDIR + "/.local/share") ) {
						if ( !Directory.Exists(HOMEDIR + "/.local") )			
							Directory.CreateDirectory(HOMEDIR + "/.local");
						Directory.CreateDirectory(HOMEDIR + "/.local/share");
					}
					Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax");
				}
				Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax/spool");
				Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax/spool/doneq");
				Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax/spool/recq");
			}
			
			// Clean out the spool/tif directory - incoming faxes
			if ( !Directory.Exists(HOMEDIR + "/.local/share/gfax/spool/tif")) {
				Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax/spool/tif");
			} else {
				Directory.Delete(HOMEDIR + "/.local/share/gfax/spool/tif/", true);
				Directory.CreateDirectory(HOMEDIR + "/.local/share/gfax/spool/tif");
			}
			
			
			
			// Initialize GETTEXT
			Catalog.Init ("gfax", Defines.GNOME_LOCALE_DIR);

			// handle command line args ourselves					
			for (int i=0; i < args.Length; i++ ) {
				//Console.WriteLine("{0} {1}", i, args[i]);
				switch (args[i])
				{
					case "--help" :
								Console.WriteLine (Catalog.GetString("Gfax help..."));
								Console.WriteLine ("Gfax spool dir -> {0}", SpoolDirectory);
								break;
					case "-f" :	// file name is present
								filename = args[i+1];
								break;
					//case "-q" :	// immediately quit after sending fax
					//			filename = args[i+1];
					//			break;
					default:
								if (File.Exists(args[i]))
									filename = args[i];
								break;
				}
			}
			
			try {
				if ( Settings.RunSetupAtStart ) {
					// Set some default preferences.
					Settings.TransmitAgent = "efax";
					Settings.SendNow = true;
					Settings.EfaxModemDevice = "ttyS0";
					Settings.RefreshQueueInterval = 15;
					Settings.RefreshQueueEnabled = true;
				}
			} catch (Exception e) { 
				//TODO  HIG love required
				G_Message gm = new G_Message(
					Catalog.GetString(
@"Gconfd cannot find your settings. 
If you are running Gfax immediately 
after an installation, you may have 
to log out and log back in again."), TOPLEVEL);
				Console.WriteLine("Exception in main.cs {0}", e);
				Environment.Exit(0);
			}

			// If we have a file name run the send dialog
			if (filename != null) {
				GfaxSend sd = new GfaxSend (filename, args);
				FileInfo f = new FileInfo(filename);
				
				// send the faxes
				if (sd.DoSend) {
					fromSendWizard = true;
					
					// Start the fax daemon if efax
					if (Settings.TransmitAgent == "efax") {
						efax = new Efax();
						efax.run_efaxd();
					}

					Fax.sendfax(filename);
					// delete the spool file (~.local/share/gfax/spool/D.*)
					if (File.Exists(String.Concat(SpoolDirectory, "/", f.Name)))
						File.Delete(String.Concat(SpoolDirectory, "/", f.Name));
						
					//if (!quitAfterSend) {
						//Gfax gf = new Gfax (filename, args);
					//}
				}
				
				// delete the spool file that gfaxlpr created if it exists
				if (File.Exists(String.Concat("/var/spool/gfax/", USER, "/", f.Name)))
					File.Delete(String.Concat("/var/spool/gfax/", USER, "/",  f.Name));
				
			}else {
			// check to see if we've run before, if so gfax will be there
						
				if ( !Directory.Exists("/var/spool/gfax/" + USER)) {
					Directory.CreateDirectory("/var/spool/gfax/" + USER);
				}
				
				FileSystemWatcher watcher = new FileSystemWatcher();
				watcher.Path = "/var/spool/gfax/" + USER;
			
				watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite 
        	   		| NotifyFilters.FileName | NotifyFilters.DirectoryName;
        		// Only watch text files.
        		watcher.Filter = "*";
				watcher.Created += new FileSystemEventHandler(TIOnChanged);
				// Begin watching.
        		watcher.EnableRaisingEvents = true;

				// Start the fax daemon if efax
				if (Settings.TransmitAgent == "efax") {
					efax = new Efax();
					efax.run_efaxd();
				}

				Gfax gf = new Gfax (filename, args);
			}
			
		} 
		
		// Define the event handlers.
    	private static void TIOnChanged(object source, FileSystemEventArgs e)
    	{
        	// Specify what is done when a file is changed, created, or deleted.
       		Console.WriteLine("File---: " +  e.FullPath + " " + e.ChangeType);
			//GfaxSend sd = new GfaxSend ("/home/george/log.txt", pargs);
    		
    		quitAfterSend = true;
    		
    		ProcessStartInfo pidInfo = new ProcessStartInfo();
			pidInfo.FileName = "gfax";

			pidInfo.Arguments = String.Concat("-f ",e.FullPath + " " + e.ChangeType);
			
			if (Settings.Faxtracing == true) {
				if (Settings.TransmitAgent == "efax") {
					Console.WriteLine("[Efax.send_init]\n  {0}", pidInfo.Arguments);
				}
			}
			
			System.Diagnostics.Process pid = System.Diagnostics.Process.Start(pidInfo);
			pid.WaitForExit();

		
		}

	}
}