~noskcaj/ubuntu/trusty/gnome-documents/3.10.2

« back to all changes in this revision

Viewing changes to src/edit.js

  • Committer: Package Import Robot
  • Author(s): Andreas Henriksson, Thomas Bechtold
  • Date: 2013-04-04 13:32:08 UTC
  • mfrom: (3.1.4 experimental)
  • Revision ID: package-import@ubuntu.com-20130404133208-n19gqczi05z31ogb
Tags: 3.8.0-1
[ Thomas Bechtold ]
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 2013 Red Hat, Inc.
 
3
 *
 
4
 * Gnome Documents is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by the
 
6
 * Free Software Foundation; either version 2 of the License, or (at your
 
7
 * option) any later version.
 
8
 *
 
9
 * Gnome Documents is distributed in the hope that it will be useful, but
 
10
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 
11
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 
12
 * for more details.
 
13
 *
 
14
 * You should have received a copy of the GNU General Public License along
 
15
 * with Gnome Documents; if not, write to the Free Software Foundation,
 
16
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
17
 *
 
18
 */
 
19
 
 
20
const WebKit = imports.gi.WebKit;
 
21
const Soup = imports.gi.Soup;
 
22
const Gd = imports.gi.Gd;
 
23
const GdPrivate = imports.gi.GdPrivate;
 
24
const Gdk = imports.gi.Gdk;
 
25
const Gio = imports.gi.Gio;
 
26
const GLib = imports.gi.GLib;
 
27
const Gtk = imports.gi.Gtk;
 
28
const _ = imports.gettext.gettext;
 
29
 
 
30
const Lang = imports.lang;
 
31
const Mainloop = imports.mainloop;
 
32
const Signals = imports.signals;
 
33
 
 
34
const Application = imports.application;
 
35
const MainToolbar = imports.mainToolbar;
 
36
const Searchbar = imports.searchbar;
 
37
const Utils = imports.utils;
 
38
const View = imports.view;
 
39
const WindowMode = imports.windowMode;
 
40
const Documents = imports.documents;
 
41
 
 
42
const _BLANK_URI = "about:blank";
 
43
 
 
44
const EditView = new Lang.Class({
 
45
    Name: 'EditView',
 
46
 
 
47
    _init: function() {
 
48
        this._uri = null;
 
49
 
 
50
        this.widget = new Gtk.Overlay();
 
51
 
 
52
        this._scrolledWindow = new Gtk.ScrolledWindow({ hexpand: true,
 
53
                                                        vexpand: true });
 
54
        this.widget.get_style_context().add_class('documents-scrolledwin');
 
55
        this.widget.add(this._scrolledWindow);
 
56
 
 
57
        this._session = WebKit.get_default_session ();
 
58
        Soup.Session.prototype.add_feature.call(this._session, new Soup.ProxyResolverDefault());
 
59
        Soup.Session.prototype.remove_feature.call(this._session, new Soup.CookieJar());
 
60
        let jarfile = GLib.build_filenamev([GLib.get_user_cache_dir(), 'gnome-documents', 'cookies.sqlite']);
 
61
        this._cookieJar = new Soup.CookieJarDB({ filename: jarfile, read_only: false });
 
62
        Soup.Session.prototype.add_feature.call(this._session, this._cookieJar);
 
63
 
 
64
        this._progressBar = new Gtk.ProgressBar({ halign: Gtk.Align.FILL,
 
65
                                                  valign: Gtk.Align.START });
 
66
        this._progressBar.get_style_context().add_class('osd');
 
67
        this.widget.add_overlay(this._progressBar);
 
68
 
 
69
        this._createView();
 
70
 
 
71
        this.widget.show_all();
 
72
 
 
73
        this._editAction = Application.application.lookup_action('edit-current');
 
74
        this._editAction.enabled = false;
 
75
        this._editAction.connect('activate', Lang.bind(this,
 
76
            function() {
 
77
                let doc = Application.documentManager.getActiveItem();
 
78
                if (!doc)
 
79
                    return;
 
80
                Application.modeController.setWindowMode(WindowMode.WindowMode.EDIT);
 
81
                this.setUri (doc.uri);
 
82
            }));
 
83
 
 
84
        this._viewAction = Application.application.lookup_action('view-current');
 
85
        this._viewAction.enabled = false;
 
86
        this._viewAction.connect('activate', Lang.bind(this,
 
87
            function() {
 
88
                Application.modeController.setWindowMode(WindowMode.WindowMode.PREVIEW);
 
89
            }));
 
90
 
 
91
        Application.documentManager.connect('load-started',
 
92
                                            Lang.bind(this, this._onLoadStarted));
 
93
        Application.documentManager.connect('load-finished',
 
94
                                            Lang.bind(this, this._onLoadFinished));
 
95
 
 
96
    },
 
97
 
 
98
    _onLoadStarted: function() {
 
99
        this._editAction.enabled = false;
 
100
        this._viewAction.enabled = false;
 
101
    },
 
102
 
 
103
    _onLoadFinished: function(manager, doc, docModel) {
 
104
        if (doc.uri) {
 
105
            if (doc instanceof Documents.GoogleDocument)
 
106
                this._editAction.enabled = true;
 
107
            this._viewAction.enabled = true;
 
108
        }
 
109
    },
 
110
 
 
111
    _createView: function() {
 
112
        this.view = new WebKit.WebView();
 
113
        this._scrolledWindow.add(this.view);
 
114
        this.view.show();
 
115
        this.view.connect('notify::progress', Lang.bind(this, this._onProgressChanged));
 
116
    },
 
117
 
 
118
    _isLoading: function() {
 
119
        let status = this.view.load_status;
 
120
        if ((status == WebKit.LoadStatus.finished
 
121
            || status == WebKit.LoadStatus.failed)
 
122
            && status != WebKit.LoadStatus.provisional)
 
123
            return false;
 
124
 
 
125
        return status != WebKit.LoadStatus.finished
 
126
            && status != WebKit.LoadStatus.failed;
 
127
    },
 
128
 
 
129
    _onProgressChanged: function() {
 
130
        if (!this.view.uri || this.view.uri == _BLANK_URI)
 
131
            return;
 
132
 
 
133
        let progress = this.view.progress;
 
134
        let loading = this._isLoading();
 
135
 
 
136
        if (progress == 1.0 || !loading) {
 
137
            if (!this._timeoutId)
 
138
                this._timeoutId = Mainloop.timeout_add(500, Lang.bind(this, this._onTimeoutExpired));
 
139
        } else {
 
140
            if (this._timeoutId) {
 
141
                Mainloop.source_remove(this._timeoutId);
 
142
                this._timeoutId = 0;
 
143
            }
 
144
            this._progressBar.show();
 
145
        }
 
146
        let value = 0.0
 
147
        if (loading || progress == 1.0)
 
148
            value = progress;
 
149
        this._progressBar.fraction = value;
 
150
    },
 
151
 
 
152
    _onTimeoutExpired: function() {
 
153
        this._timeoutId = 0;
 
154
        this._progressBar.hide();
 
155
        return false;
 
156
    },
 
157
 
 
158
    setUri: function(uri) {
 
159
        if (this._uri == uri)
 
160
            return;
 
161
 
 
162
        if (!uri)
 
163
            uri = _BLANK_URI;
 
164
 
 
165
        this._uri = uri;
 
166
        this.view.load_uri (uri);
 
167
    },
 
168
 
 
169
    getUri: function() {
 
170
        return this._uri;
 
171
    },
 
172
});
 
173
Signals.addSignalMethods(EditView.prototype);
 
174
 
 
175
const EditToolbar = new Lang.Class({
 
176
    Name: 'EditToolbar',
 
177
    Extends: MainToolbar.MainToolbar,
 
178
 
 
179
    _init: function(editView) {
 
180
        this._editView = editView;
 
181
 
 
182
        this.parent();
 
183
 
 
184
        // back button, on the left of the toolbar
 
185
        let backButton = this.addBackButton();
 
186
        backButton.connect('clicked', Lang.bind(this,
 
187
            function() {
 
188
                Application.documentManager.setActiveItem(null);
 
189
            }));
 
190
 
 
191
        let viewButton = new Gd.HeaderSimpleButton({ label: _("View"),
 
192
                                                     action_name: 'app.view-current' });
 
193
        viewButton.get_style_context().add_class('suggested-action');
 
194
        this.toolbar.pack_end(viewButton);
 
195
 
 
196
        this._setToolbarTitle();
 
197
        this.widget.show_all();
 
198
    },
 
199
 
 
200
    createSearchbar: function() {
 
201
    },
 
202
 
 
203
    handleEvent: function(event) {
 
204
        return false;
 
205
    },
 
206
 
 
207
    _setToolbarTitle: function() {
 
208
        let primary = null;
 
209
        let doc = Application.documentManager.getActiveItem();
 
210
 
 
211
        if (doc)
 
212
            primary = doc.name;
 
213
 
 
214
        this.toolbar.set_title(primary);
 
215
    }
 
216
});