My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
/* Copyright (c) 2007 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Ipc;
using Microsoft.Win32;
using Google.GData.Documents;
using Google.GData.Client;

namespace DocListUploader
{
/// <summary>
/// The main form of the uploader, controls most of the service interaction.
/// It is not actually displayed to the user.
/// </summary>
public partial class HiddenForm : Form
{
//A child form for the user to interact with.
private OptionsForm optionsForm;
//Keeps track of our logged in state.
public bool loggedIn = false;
//A connection with the DocList API.
private DocumentsService service;
//The name of the shell context menu option.
private const string KEY_NAME = "Send to Google Docs";
//Keeps track of if we've minimized the OptionsForm before.
private bool firstMinimize = true;
//The timer in milliseconds to display balloon tips.
private const int BALLOON_TIMER = 10000;
//The most recently uploaded document.
private DocumentEntry lastUploadEntry = null;
//Keeps track of if the last balloon tip was an upload complete message.
private bool lastToolTipWasUpload = false;

/// <summary>
/// Constructor for the form.
/// </summary>
public HiddenForm()
{
InitializeComponent();
optionsForm = new OptionsForm(this);
ListenForSuccessor();
}

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
bool firstInstance;
Mutex mutex = new Mutex(true, "Local\\DocListUploader", out firstInstance);
if (!firstInstance)
{
//If we have an argument, we were most likely called by
//the shell context menu
if (args.Length == 1)
{
NotifyPredecessor(args[0]);
}
return;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new HiddenForm());
}

/// <summary>
/// This is run by the first instance of the program in order
/// to accept information from later instances using the
/// .NET Framework's IPC mechanism.
/// </summary>
public void ListenForSuccessor()
{
IpcServerChannel serverChannel = new IpcServerChannel("DocListUploader");
ChannelServices.RegisterChannel(serverChannel, false);

RemoteMessage remoteMessage = new RemoteMessage(this);
RemotingServices.Marshal(remoteMessage,"FirstInstance");

}

/// <summary>
/// This is used by later instances of the program when
/// executed from the shell's context menu to pass along
/// the path of a file that will be uploaded. This message
/// is passed using the .NET Framework's IPC mechanism.
/// </summary>
/// <param name="file">The file to be uploaded</param>
public static void NotifyPredecessor(string file)
{
IpcClientChannel clientChannel = new IpcClientChannel();
ChannelServices.RegisterChannel(clientChannel, false);

RemoteMessage message = (RemoteMessage) Activator.GetObject(typeof(RemoteMessage), "ipc://DocListUploader/FirstInstance");

if (!message.Equals(null))
{
message.SendMessage(file);
}
}

/// <summary>
/// Authenticates to Google servers
/// </summary>
/// <param name="username">The user's username (e-mail)</param>
/// <param name="password">The user's password</param>
/// <exception cref="AuthenticationException">Thrown on invalid credentials.</exception>
public void Login(string username, string password)
{
if(loggedIn) {
throw new ApplicationException("Already logged in.");
}
try
{
service = new DocumentsService("DocListUploader");
((GDataRequestFactory) service.RequestFactory).KeepAlive = false;
service.setUserCredentials(username, password);
//force the service to authenticate
DocumentsListQuery query = new DocumentsListQuery();
query.NumberToRetrieve = 1;
service.Query(query);
loggedIn = true;
}
catch(AuthenticationException e)
{
loggedIn = false;
service = null;
throw e;
}
}

/// <summary>
/// Logs the user out of Google Docs.
/// </summary>
public void Logout()
{
loggedIn = false;
service = null;
}

/// <summary>
/// Retrieves a list of documents from the server.
/// </summary>
/// <returns>The list of documents as a DocumentsFeed.</returns>
public DocumentsFeed GetDocs()
{
DocumentsListQuery query = new DocumentsListQuery();
DocumentsFeed feed = service.Query(query);
return feed;
}

/// <summary>
/// Some error handling around the file upload method. Used for handling files from the context menu.
/// </summary>
/// <param name="file">The file to upload.</param>
public void HandleUpload(string file)
{
if (!loggedIn)
{
MessageBox.Show("Please log in before uploading documents", "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
DocListNotifyIcon.ShowBalloonTip(BALLOON_TIMER, "Uploading", "Uploading " + file, ToolTipIcon.Info);
lastToolTipWasUpload = false;
UploadFile(file);
DocListNotifyIcon.ShowBalloonTip(BALLOON_TIMER, "Upload Success", "Successfully uploaded " + file, ToolTipIcon.Info);
lastToolTipWasUpload = true;
optionsForm.UpdateDocList();
}
catch (ArgumentException)
{
DocListNotifyIcon.ShowBalloonTip(BALLOON_TIMER, "Upload Error", "Error: unable to upload the file: '" + file + "'. It is not one of the valid types.", ToolTipIcon.Error);
lastToolTipWasUpload = false;
}
catch (Exception ex)
{
DocListNotifyIcon.ShowBalloonTip(BALLOON_TIMER, "Upload Error", "Error: unable to upload the file: '" + file + "'. " + ex.Message, ToolTipIcon.Error);
lastToolTipWasUpload = false;
}
}

/// <summary>
/// Uploads the file to Google Docs
/// </summary>
/// <param name="fileName">The file with path to upload</param>
/// <exception cref="ApplicationException">Thrown when user isn't logged in.</exception>
public void UploadFile(string fileName)
{
if (!loggedIn)
{
throw new ApplicationException("Need to be logged in to upload documents.");
}
else
{
lastUploadEntry = service.UploadDocument(fileName, null);
}
}

/// <summary>
/// Called when the OptionsForm is minimized/hidden.
/// </summary>
public void HandleMinimize()
{
if (firstMinimize)
{
DocListNotifyIcon.ShowBalloonTip(BALLOON_TIMER, "DocList Uploader", "Uploader now running in the system tray.", ToolTipIcon.Info);
lastToolTipWasUpload = false;
firstMinimize = false;
}

}

/// <summary>
/// Adds the application to the shell's context menu by
/// creating a registry key.
/// </summary>
public void Register()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Classes\\*\\shell\\" + KEY_NAME + "\\command");

if (key == null)
{
key = Registry.CurrentUser.CreateSubKey("Software\\Classes\\*\\shell\\" + KEY_NAME + "\\command");
}

key.SetValue("", Application.ExecutablePath + " \"%1\"");
}

/// <summary>
/// Removes the application from the shell's context menu
/// by deleting a registry key.
/// </summary>
public void UnRegister()
{
RegistryKey key = Registry.CurrentUser.OpenSubKey("Software\\Classes\\*\\shell\\" + KEY_NAME);

if (key != null)
{
Registry.CurrentUser.DeleteSubKeyTree("Software\\Classes\\*\\shell\\" + KEY_NAME);
}
}

/// <summary>
/// Displays the OptionsForm.
/// </summary>
private void ShowOptionsWindow()
{
optionsForm.Show();
if (optionsForm.WindowState == FormWindowState.Minimized)
{
optionsForm.WindowState = FormWindowState.Normal;
}
}

private void DocListNotifyIcon_DoubleClick(object sender, EventArgs e)
{
optionsForm.Show();
}

private void CloseMenuItem_Click(object sender, EventArgs e)
{
Close();
}

private void HiddenForm_Load(object sender, EventArgs e)
{
Hide();
ShowOptionsWindow();
}

private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowOptionsWindow();
}

private void HiddenForm_FormClosing(object sender, FormClosingEventArgs e)
{
UnRegister();
}

private void DocListNotifyIcon_MouseClick(object sender, MouseEventArgs e)
{
//A single left click should be the same as a double-click.
if (e.Button == MouseButtons.Left)
{
ShowOptionsWindow();
}
}


private void DocListNotifyIcon_BalloonTipClicked(object sender, EventArgs e)
{
if (lastUploadEntry != null && lastToolTipWasUpload)
{
//display the last uploaded document in the browser.
try
{
Process.Start(lastUploadEntry.AlternateUri.ToString());
}
catch (Win32Exception)
{
//nothing is registered to handle URLs, so let's use IE!
Process.Start("IExplore.exe", lastUploadEntry.AlternateUri.ToString());
}
}
}


}
}

Change log

r546 by api.jfisher on May 9, 2008   Diff
Fixing a bug where Windows has no default
URL handler set.
Go to: 
Project members, sign in to write a code review

Older revisions

r474 by api.jfisher on Feb 22, 2008   Diff
Fixed bug with UAC under Vista
modifying the Registry.
r362 by api.jfisher on Jan 8, 2008   Diff
Adding sample Documents List Uploader
utility.
All revisions of this file

File info

Size: 12184 bytes, 336 lines
Powered by Google Project Hosting