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

« back to all changes in this revision

Viewing changes to src/sources.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) 2011 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
 
 * Author: Cosimo Cecchi <cosimoc@redhat.com>
19
 
 *
20
 
 */
21
 
 
22
 
const Lang = imports.lang;
23
 
 
24
 
const Gio = imports.gi.Gio;
25
 
const Goa = imports.gi.Goa;
26
 
const _ = imports.gettext.gettext;
27
 
 
28
 
const Global = imports.global;
29
 
const Manager = imports.manager;
30
 
 
31
 
const SourceStock = {
32
 
    ALL: 'all',
33
 
    LOCAL: 'local'
34
 
}
35
 
 
36
 
const Source = new Lang.Class({
37
 
    Name: 'Source',
38
 
 
39
 
    _init: function(params) {
40
 
        this.id = null;
41
 
        this.name = null;
42
 
        this.icon = null;
43
 
 
44
 
        if (params.object) {
45
 
            this.object = params.object;
46
 
            let account = params.object.get_account();
47
 
 
48
 
            this.id = 'gd:goa-account:' + account.id;
49
 
            this.name = account.provider_name;
50
 
            this.icon = Gio.icon_new_for_string(account.provider_icon);
51
 
        } else {
52
 
            this.id = params.id;
53
 
            this.name = params.name;
54
 
        }
55
 
 
56
 
        this.builtin = params.builtin;
57
 
    },
58
 
 
59
 
    getFilter: function() {
60
 
        if (this.id == SourceStock.LOCAL)
61
 
            return Global.queryBuilder.buildFilterLocal();
62
 
 
63
 
        if (this.id == SourceStock.ALL)
64
 
            return '(' + Global.queryBuilder.buildFilterLocal() + ' || '
65
 
                    + Global.queryBuilder.buildFilterNotLocal() + ')';
66
 
 
67
 
        return this._buildFilterResource();
68
 
    },
69
 
 
70
 
    _buildFilterResource: function() {
71
 
        let filter = '(false)';
72
 
 
73
 
        if (!this.builtin)
74
 
            filter = ('(nie:dataSource(?urn) = "%s")').format(this.id);
75
 
 
76
 
        return filter;
77
 
    }
78
 
});
79
 
 
80
 
const SourceManager = new Lang.Class({
81
 
    Name: 'SourceManager',
82
 
    Extends: Manager.BaseManager,
83
 
 
84
 
    _init: function() {
85
 
        this.parent(_("Sources"));
86
 
 
87
 
        // Translators: this refers to documents
88
 
        let source = new Source({ id: SourceStock.ALL,
89
 
                                  name: _("All"),
90
 
                                  builtin: true });
91
 
        this.addItem(source);
92
 
 
93
 
        // Translators: this refers to local documents
94
 
        source = new Source({ id: SourceStock.LOCAL,
95
 
                              name: _("Local"),
96
 
                              builtin: true });
97
 
        this.addItem(source);
98
 
 
99
 
        Global.goaClient.connect('account-added', Lang.bind(this, this._refreshGoaAccounts));
100
 
        Global.goaClient.connect('account-changed', Lang.bind(this, this._refreshGoaAccounts));
101
 
        Global.goaClient.connect('account-removed', Lang.bind(this, this._refreshGoaAccounts));
102
 
 
103
 
        this._refreshGoaAccounts();
104
 
        this.setActiveItemById(SourceStock.ALL);
105
 
    },
106
 
 
107
 
    _refreshGoaAccounts: function() {
108
 
        let newItems = {};
109
 
        let accounts = Global.goaClient.get_accounts();
110
 
 
111
 
        accounts.forEach(Lang.bind(this,
112
 
            function(object) {
113
 
                if (!object.get_account())
114
 
                    return;
115
 
 
116
 
                if (!object.get_documents())
117
 
                    return;
118
 
 
119
 
                let source = new Source({ object: object });
120
 
                newItems[source.id] = source;
121
 
            }));
122
 
 
123
 
        this.processNewItems(newItems);
124
 
    },
125
 
 
126
 
    hasOnlineSources: function() {
127
 
        let hasOnline = false;
128
 
        this.forEachItem(
129
 
            function(source) {
130
 
                if (source.object)
131
 
                    hasOnline = true;
132
 
            });
133
 
 
134
 
        return hasOnline;
135
 
    },
136
 
 
137
 
    hasProviderType: function(providerType) {
138
 
        let found = false;
139
 
        this.forEachItem(Lang.bind(this,
140
 
            function(source) {
141
 
                if (!source.object)
142
 
                    return;
143
 
 
144
 
                let account = source.object.get_account();
145
 
                if (!account)
146
 
                    return;
147
 
 
148
 
                if (found)
149
 
                    return;
150
 
 
151
 
                if (account.provider_type == providerType)
152
 
                    found = true;
153
 
            }));
154
 
 
155
 
        return found;
156
 
    }
157
 
});