~ubuntu-branches/ubuntu/lucid/tomboy/lucid-proposed

« back to all changes in this revision

Viewing changes to Tomboy/Addins/FileSystemSyncService/FileSystemSyncServiceAddin.cs

Tags: upstream-0.9.4
ImportĀ upstreamĀ versionĀ 0.9.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
                // TODO: Extract most of the code here and build GenericSyncServiceAddin
14
14
                // that supports a field, a username, and password.  This could be useful
15
15
                // in quickly building SshSyncServiceAddin, FtpSyncServiceAddin, etc.
16
 
                
 
16
 
17
17
                private Entry pathEntry;
18
18
                private string path;
19
19
                private bool initialized = false;
20
 
                
 
20
 
21
21
                /// <summary>
22
22
                /// Called as soon as Tomboy needs to do anything with the service
23
23
                /// </summary>
25
25
                {
26
26
                        initialized = true;
27
27
                }
28
 
                
 
28
 
29
29
                public override void Shutdown ()
30
30
                {
31
31
                        // Do nothing for now
32
32
                }
33
 
                
 
33
 
34
34
                public override bool Initialized {
35
 
                        get { return initialized; }
 
35
                        get {
 
36
                                return initialized;
 
37
                        }
36
38
                }
37
39
 
38
40
 
46
48
                public override SyncServer CreateSyncServer ()
47
49
                {
48
50
                        SyncServer server = null;
49
 
                        
 
51
 
50
52
                        string syncPath;
51
53
                        if (GetConfigSettings (out syncPath)) {
52
54
                                path = syncPath;
62
64
                        } else {
63
65
                                throw new InvalidOperationException ("FileSystemSyncServiceAddin.CreateSyncServer () called without being configured");
64
66
                        }
65
 
                        
 
67
 
66
68
                        return server;
67
69
                }
68
 
                
 
70
 
69
71
                public override void PostSyncCleanup ()
70
72
                {
71
73
                        // Nothing to do
72
74
                }
73
 
                
 
75
 
74
76
                /// <summary>
75
77
                /// Creates a Gtk.Widget that's used to configure the service.  This
76
78
                /// will be used in the Synchronization Preferences.  Preferences should
80
82
                public override Gtk.Widget CreatePreferencesControl ()
81
83
                {
82
84
                        Gtk.Table table = new Gtk.Table (1, 3, false);
83
 
                        
 
85
 
84
86
                        // Read settings out of gconf
85
87
                        string syncPath;
86
88
                        if (GetConfigSettings (out syncPath) == false)
87
89
                                syncPath = string.Empty;
88
90
 
89
 
                        Label l = new Label (Catalog.GetString ("Folder Path:"));
 
91
                        Label l = new Label (Catalog.GetString ("_Folder Path:"));
90
92
                        l.Xalign = 1;
91
93
                        table.Attach (l, 0, 1, 0, 1);
92
 
                        
 
94
 
93
95
                        pathEntry = new Entry ();
94
96
                        pathEntry.Text = syncPath;
95
97
                        table.Attach (pathEntry, 1, 2, 0, 1);
96
 
                        
 
98
                        l.MnemonicWidget = pathEntry;
 
99
 
97
100
                        Image browseImage = new Image (Stock.Open, IconSize.Button);
98
 
                        Label browseLabel = new Label (Catalog.GetString ("Browse..."));
99
 
                        
 
101
                        Label browseLabel = new Label (Catalog.GetString ("_Browse..."));
 
102
 
100
103
                        HBox browseBox = new HBox (false, 0);
101
104
                        browseBox.PackStart (browseImage);
102
 
                        browseBox.PackStart (browseLabel);                      
103
 
                        
 
105
                        browseBox.PackStart (browseLabel);
 
106
 
104
107
                        Button browseButton = new Button ();
105
108
                        browseButton.Add (browseBox);
 
109
                        browseLabel.MnemonicWidget = browseButton;
106
110
                        browseButton.Clicked += OnBrowseButtonClicked;
107
111
                        table.Attach (browseButton, 2, 3, 0, 1, AttachOptions.Shrink, AttachOptions.Expand, 0, 0);
108
 
                        
 
112
 
109
113
                        table.ShowAll ();
110
114
                        return table;
111
115
                }
112
 
                
 
116
 
113
117
                private void OnBrowseButtonClicked (object sender, EventArgs args)
114
118
                {
115
119
                        FileChooserDialog chooserDlg =
116
 
                                new FileChooserDialog (Catalog.GetString ("Select Synchronization Folder..."),
117
 
                                                       null,
118
 
                                                       FileChooserAction.SelectFolder,
119
 
                                                       Stock.Cancel, ResponseType.Cancel,
120
 
                                                       Stock.Ok, ResponseType.Ok);
 
120
                                new FileChooserDialog (Catalog.GetString ("Select Synchronization Folder..."),
 
121
                                                       null,
 
122
                                                       FileChooserAction.SelectFolder,
 
123
                                                       Stock.Cancel, ResponseType.Cancel,
 
124
                                                       Stock.Ok, ResponseType.Ok);
121
125
                        chooserDlg.DefaultResponse = ResponseType.Cancel;
122
126
                        chooserDlg.SetFilename (pathEntry.Text);
123
 
                        
 
127
 
124
128
                        ResponseType response = (ResponseType) chooserDlg.Run ();
125
 
                        
 
129
 
126
130
                        if (response == ResponseType.Ok)
127
131
                                pathEntry.Text = chooserDlg.Filename;
128
 
                        
 
132
 
129
133
                        chooserDlg.Destroy ();
130
134
                }
131
 
                
 
135
 
132
136
                /// <summary>
133
137
                /// The Addin should verify and check the connection to the service
134
138
                /// when this is called.  If verification and connection is successful,
137
141
                public override bool SaveConfiguration ()
138
142
                {
139
143
                        string syncPath = pathEntry.Text.Trim ();
140
 
                        
 
144
 
141
145
                        if (syncPath == string.Empty) {
142
146
                                // TODO: Figure out a way to send the error back to the client
143
147
                                Logger.Debug ("The path is empty");
144
148
                                throw new TomboySyncException (Catalog.GetString ("Folder path field is empty."));
145
149
                        }
146
 
                        
 
150
 
147
151
                        // Attempt to create the path and fail if we can't
148
152
                        if (Directory.Exists (syncPath) == false) {
149
153
                                try {
151
155
                                } catch (Exception e) {
152
156
                                        Logger.Debug ("Could not create \"{0}\": {1}", path, e.Message);
153
157
                                        throw new TomboySyncException (Catalog.GetString ("Specified folder path does not exist, " +
154
 
                                                                                          "and Tomboy was unable to create it."));
 
158
                                                                       "and Tomboy was unable to create it."));
155
159
                                }
156
160
                        } else {
157
161
                                // Test creating/writing/deleting a file
159
163
                                string testPathBase = Path.Combine (syncPath, "test");
160
164
                                string testPath = testPathBase;
161
165
                                int count = 0;
162
 
                                
 
166
 
163
167
                                // Get unique new file name
164
168
                                while (File.Exists (testPath))
165
169
                                        testPath = testPathBase + (++count).ToString ();
166
 
                                
 
170
 
167
171
                                // Test ability to create and write
168
172
                                string testLine = "Testing write capabilities.";
169
173
                                using (FileStream fs = File.Create (testPath)) {
170
174
                                        StreamWriter writer = new StreamWriter (fs);
171
175
                                        writer.WriteLine (testLine);
172
176
                                }
173
 
                                
 
177
 
174
178
                                // Test ability to read
175
179
                                bool testFileFound = false;
176
180
                                foreach (string filePath in Directory.GetFiles (syncPath))
177
 
                                        if (filePath == testPath) {
178
 
                                                testFileFound = true;
179
 
                                                break;
180
 
                                        }
 
181
                                if (filePath == testPath) {
 
182
                                        testFileFound = true;
 
183
                                        break;
 
184
                                }
181
185
                                if (!testFileFound)
182
186
                                        ; // TODO: Throw TomboySyncException
183
187
                                using (StreamReader reader = new StreamReader (testPath)) {
184
188
                                        if (reader.ReadLine () != testLine)
185
189
                                                ; // TODO: Throw TomboySyncException
186
190
                                }
187
 
                                
 
191
 
188
192
                                // Test ability to delete
189
193
                                File.Delete (testPath);
190
194
                        }
191
 
                        
 
195
 
192
196
                        path = syncPath;
193
 
                        
 
197
 
194
198
                        // TODO: Try to create and delete a file.  If it fails, this should fail
195
199
                        Preferences.Set (Preferences.SYNC_LOCAL_PATH, path);
196
 
                        
 
200
 
197
201
                        return true;
198
202
                }
199
203
 
204
208
                {
205
209
                        Preferences.Set (Preferences.SYNC_LOCAL_PATH, string.Empty);
206
210
                }
207
 
                
 
211
 
208
212
                /// <summary>
209
213
                /// Returns whether the addin is configured enough to actually be used.
210
214
                /// </summary>
212
216
                {
213
217
                        get {
214
218
                                string syncPath = Preferences.Get (Preferences.SYNC_LOCAL_PATH) as String;
215
 
                                
 
219
 
216
220
                                if (syncPath != null && syncPath != string.Empty) {
217
221
                                        return true;
218
222
                                }
219
 
                                
 
223
 
220
224
                                return false;
221
225
                        }
222
226
                }
223
 
                
 
227
 
224
228
                /// <summary>
225
229
                /// The name that will be shown in the preferences to distinguish
226
230
                /// between this and other SyncServiceAddins.
242
246
                                return "local";
243
247
                        }
244
248
                }
245
 
                
 
249
 
246
250
                /// <summary>
247
251
                /// Returns true if the addin has all the supporting libraries installed
248
252
                /// on the machine or false if the proper environment is not available.
253
257
                /// </summary>
254
258
                public override bool IsSupported
255
259
                {
256
 
                        get { return true; }
 
260
                        get {
 
261
                                return true;
 
262
                        }
257
263
                }
258
 
                
 
264
 
259
265
                #region Private Methods
260
266
                /// <summary>
261
267
                /// Get config settings
267
273
                        if (syncPath != null && syncPath != string.Empty) {
268
274
                                return true;
269
275
                        }
270
 
                        
 
276
 
271
277
                        return false;
272
278
                }
273
279
                #endregion // Private Methods