~sschuhmann/scratch/gtk-stack

« back to all changes in this revision

Viewing changes to src/Services/Document.vala

  • Committer: Steffen Schuhmann
  • Date: 2014-04-14 20:05:11 UTC
  • mfrom: (1253.2.29 scratch)
  • Revision ID: schuhmannsteffen+bazaar@mailbox.org-20140414200511-w5ofz70pm1dvu8pu
Merged upstream to resolve mergingconflicts

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
 
22
22
using Granite.Widgets;
23
23
using Scratch.Widgets;
 
24
#if HAVE_ZEITGEIST
24
25
using Zeitgeist;
 
26
#endif
25
27
 
26
28
namespace Scratch.Services {
27
29
 
50
52
        
51
53
        // It is used to load file content on focusing
52
54
        private bool loaded = false;
 
55
        private bool has_started_loading = false;
53
56
        
 
57
#if HAVE_ZEITGEIST
54
58
        // Zeitgeist integration
55
59
        private ZeitgeistLogger zg_log = new ZeitgeistLogger();
 
60
#endif
56
61
 
57
62
        // It is used to not mark files as changed on load
58
63
        private ulong onchange_handler_id = 0;
93
98
                            Source.remove (timeout_saving);
94
99
                            timeout_saving = -1;
95
100
                        }
96
 
                        timeout_saving = Timeout.add (250, () => {
 
101
                        timeout_saving = Timeout.add (3000, () => {
97
102
                            save ();
98
103
                            timeout_saving = -1;
99
104
                            return false;
131
136
                            Source.remove (timeout_saving);
132
137
                            timeout_saving = -1;
133
138
                        }
134
 
                        timeout_saving = Timeout.add (250, () => {
 
139
                        timeout_saving = Timeout.add (3000, () => {
135
140
                            save ();
136
141
                            timeout_saving = -1;
137
142
                            return false;
154
159
                return false;
155
160
            });
156
161
 
 
162
            // Focus out event for SourceView
 
163
            this.source_view.focus_out_event.connect (() => {
 
164
                if (settings.autosave) {
 
165
                    save ();
 
166
                }
 
167
 
 
168
                return false;
 
169
            });
 
170
 
157
171
            // Change syntax highlight
158
172
            this.source_view.change_syntax_highlight_from_file (this.file);
159
173
 
160
174
            // Stop loading
161
175
            this.working = false;
162
176
 
 
177
#if HAVE_ZEITGEIST
163
178
            // Zeitgeist integration
164
179
            zg_log.open_insert (file.get_uri (), get_mime_type ());
 
180
#endif
165
181
 
166
182
            // Grab focus
167
183
            this.source_view.grab_focus ();
188
204
 
189
205
                dialog.use_markup = true;
190
206
 
191
 
                    dialog.text = ("<b>" + _("Save changes to document %s before closing?") +
192
 
                                    "</b>").printf (this.get_basename ());
 
207
                dialog.text = ("<b>" + _("Save changes to document %s before closing?") +
 
208
                               "</b>").printf (this.get_basename ());
193
209
                dialog.text += "\n\n" +
194
210
                            _("If you don't save, changes from the last 4 seconds will be permanently lost.");
195
211
 
204
220
                int response = dialog.run ();
205
221
                switch (response) {
206
222
                    case Gtk.ResponseType.CANCEL:
 
223
                    case Gtk.ResponseType.DELETE_EVENT:
207
224
                        ret_value = false;
208
225
                        break;
209
226
                    case Gtk.ResponseType.YES:
218
235
            }
219
236
 
220
237
            if (file != null) {
 
238
                // Save the changes that were not saved during typing breaks
 
239
                save ();
221
240
                // Delete backup copy file
222
241
                delete_backup ();
 
242
#if HAVE_ZEITGEIST
223
243
                // Zeitgeist integration
224
244
                zg_log.close_insert (file.get_uri (), get_mime_type ());
 
245
#endif
225
246
            }
226
247
            
227
248
            return ret_value;
230
251
        public bool save () {
231
252
            if (last_saved_content == get_text () && this.file != null)
232
253
                return false;
233
 
                
 
254
 
 
255
            if (!this.loaded)
 
256
                return false;
 
257
 
234
258
            // Create backup copy file if it does not still exist
235
 
            create_backup ();
 
259
            if (this.file != null)
 
260
                create_backup ();
236
261
 
237
262
            // Show save as dialog if file is null
238
263
            if (this.file == null)
246
271
                warning ("Cannot save \"%s\": %s", get_basename (), e.message);
247
272
            }
248
273
 
 
274
#if HAVE_ZEITGEIST
249
275
            // Zeitgeist integration
250
276
            zg_log.save_insert (file.get_uri (), get_mime_type ());
 
277
#endif
251
278
 
252
279
            doc_saved ();
253
280
            this.set_saved_status (true);
290
317
            this.file = new_dest;
291
318
            this.save ();
292
319
 
 
320
#if HAVE_ZEITGEIST
293
321
            // Zeitgeist integration
294
322
            zg_log.move_insert (file.get_uri (), new_dest.get_uri (), get_mime_type ());
 
323
#endif
295
324
 
296
325
            return true;
297
326
        }
457
486
        
458
487
        // Load file content
459
488
        internal void load_content () {
460
 
            if (!this.loaded) {
 
489
            if (!this.has_started_loading) {
461
490
                FileHandler.load_content_from_file.begin (file, (obj, res) => {
462
491
                    var text = FileHandler.load_content_from_file.end (res);
463
492
                    if (text == null) {
470
499
                    this.source_view.set_text (text);
471
500
                    this.last_saved_content = text;
472
501
                    this.original_content = text;
 
502
                    this.loaded = true;
473
503
                 });
474
 
                 this.loaded = true;
 
504
 
 
505
                this.has_started_loading = true;
475
506
            }
476
507
        }
477
508
        
630
661
            return this.file.query_exists ();
631
662
        }
632
663
    }
633
 
 
634
664
}
 
665