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

« back to all changes in this revision

Viewing changes to src/documents.js

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2012-06-29 13:16:35 UTC
  • mfrom: (1.1.5)
  • Revision ID: package-import@ubuntu.com-20120629131635-tkocc51xk4c37a39
Tags: 0.5.3-0ubuntu1
* New upstream release.
* debian/control.in:
  - Bump minimum GTK
  - Build-depend on libzapojit-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * Copyright (c) 2011 Red Hat, Inc.
 
2
 * Copyright (c) 2011, 2012 Red Hat, Inc.
3
3
 *
4
4
 * Gnome Documents is free software; you can redistribute it and/or modify
5
5
 * it under the terms of the GNU General Public License as published by the
19
19
 *
20
20
 */
21
21
 
 
22
const EvView = imports.gi.EvinceView;
22
23
const GdkPixbuf = imports.gi.GdkPixbuf;
23
24
const Gio = imports.gi.Gio;
24
25
const Gd = imports.gi.Gd;
27
28
const GLib = imports.gi.GLib;
28
29
const GObject = imports.gi.GObject;
29
30
const Gtk = imports.gi.Gtk;
 
31
const Zpj = imports.gi.Zpj;
30
32
const _ = imports.gettext.gettext;
31
33
 
32
34
const Lang = imports.lang;
41
43
const TrackerUtils = imports.trackerUtils;
42
44
const Utils = imports.utils;
43
45
 
44
 
function SingleItemJob(doc) {
45
 
    this._init(doc);
46
 
}
 
46
const SingleItemJob = new Lang.Class({
 
47
    Name: 'SingleItemJob',
47
48
 
48
 
SingleItemJob.prototype = {
49
49
    _init: function(urn) {
50
50
        this._urn = urn;
51
51
        this._cursor = null;
54
54
    run: function(flags, callback) {
55
55
        this._callback = callback;
56
56
 
57
 
        let query = Global.queryBuilder.buildSingleQuery(this._urn, flags);
 
57
        let query = Global.queryBuilder.buildSingleQuery(flags, this._urn);
58
58
        Global.connectionQueue.add(query.sparql, null, Lang.bind(this,
59
59
            function(object, res) {
60
60
                try {
91
91
    _emitCallback: function() {
92
92
        this._callback(this._cursor);
93
93
    }
94
 
};
95
 
 
96
 
function DeleteItemJob(urn) {
97
 
    this._init(urn);
98
 
}
99
 
 
 
94
});
 
95
 
 
96
const DeleteItemJob = new Lang.Class({
 
97
    Name: 'DeleteItemJob',
100
98
// deletes the given resource
101
 
DeleteItemJob.prototype = {
 
99
 
102
100
    _init: function(urn) {
103
101
        this._urn = urn;
104
102
    },
119
117
                    this._callback();
120
118
            }));
121
119
    }
122
 
};
123
 
 
124
 
function CollectionIconWatcher(collection) {
125
 
    this._init(collection);
126
 
}
127
 
 
128
 
CollectionIconWatcher.prototype = {
 
120
});
 
121
 
 
122
 
 
123
const CollectionIconWatcher = new Lang.Class({
 
124
    Name: 'CollectionIconWatcher',
 
125
 
129
126
    _init: function(collection) {
130
127
        this._collection = collection;
131
128
        this._pixbuf = null;
265
262
        this.destroy();
266
263
        this._start();
267
264
    }
268
 
};
 
265
});
269
266
Signals.addSignalMethods(CollectionIconWatcher.prototype);
270
267
 
271
 
function DocCommon(cursor) {
272
 
    this._init(cursor);
273
 
}
 
268
const DocCommon = new Lang.Class({
 
269
    Name: 'DocCommon',
274
270
 
275
 
DocCommon.prototype = {
276
271
    _init: function(cursor) {
277
272
        this.id = null;
278
273
        this.uri = null;
333
328
        this.favorite = cursor.get_boolean(Query.QueryColumns.FAVORITE);
334
329
 
335
330
        let mtime = cursor.get_string(Query.QueryColumns.MTIME)[0];
336
 
        let timeVal = Gd.time_val_from_iso8601(mtime)[1];
337
 
        this.mtime = timeVal.tv_sec;
 
331
        if (mtime) {
 
332
            let timeVal = GLib.time_val_from_iso8601(mtime)[1];
 
333
            this.mtime = timeVal.tv_sec;
 
334
        } else {
 
335
            this.mtime = Math.floor(GLib.get_real_time() / 1000000);
 
336
        }
338
337
 
339
338
        this.mimeType = cursor.get_string(Query.QueryColumns.MIMETYPE)[0];
340
339
        this.rdfType = cursor.get_string(Query.QueryColumns.RDFTYPE)[0];
345
344
            this.uri = '';
346
345
 
347
346
        let title = cursor.get_string(Query.QueryColumns.TITLE)[0];
 
347
        let filename = cursor.get_string(Query.QueryColumns.FILENAME)[0];
 
348
 
348
349
        if (title && title != '')
349
350
            this.name = title;
 
351
        else if (filename)
 
352
            this.name = Gd.filename_strip_extension(filename);
350
353
        else
351
 
            this.name = Gd.filename_strip_extension(
352
 
                cursor.get_string(Query.QueryColumns.FILENAME)[0]);
 
354
            this.name = '';
353
355
 
354
356
        this._sanitizeTitle();
355
357
 
603
605
            retval = '{ ?urn nie:isPartOf <' + this.id + '> }';
604
606
 
605
607
        return retval;
 
608
    },
 
609
 
 
610
    _finishLoad: function(document, callback, exception) {
 
611
        let docModel = null;
 
612
        if (exception) {
 
613
            Global.errorHandler.addLoadError(this, exception);
 
614
        } else {
 
615
            docModel = EvView.DocumentModel.new_with_document(document);
 
616
        }
 
617
 
 
618
        callback(this, docModel, exception);
606
619
    }
607
 
};
 
620
});
608
621
Signals.addSignalMethods(DocCommon.prototype);
609
622
 
610
 
function LocalDocument(cursor) {
611
 
    this._init(cursor);
612
 
}
613
 
 
614
 
LocalDocument.prototype = {
615
 
    __proto__: DocCommon.prototype,
 
623
const LocalDocument = new Lang.Class({
 
624
    Name: 'LocalDocument',
 
625
    Extends: DocCommon,
616
626
 
617
627
    _init: function(cursor) {
618
628
        this._failedThumbnailing = false;
619
629
        this._triedThumbnailing = false;
620
630
 
621
 
        DocCommon.prototype._init.call(this, cursor);
 
631
        this.parent(cursor);
622
632
 
623
633
        this.sourceName = _("Local");
624
634
 
640
650
            function(source, res) {
641
651
                try {
642
652
                    let document = Gd.pdf_loader_load_uri_finish(res);
643
 
                    callback(this, document, null);
 
653
                    this._finishLoad(document, callback, null);
644
654
                } catch (e) {
645
 
                    callback(this, null, e);
 
655
                    this._finishLoad(null, callback, e);
646
656
                }
647
657
            }));
648
658
    },
657
667
            job.run(null);
658
668
        }
659
669
    }
660
 
};
 
670
});
661
671
 
662
672
const _GOOGLE_DOCS_SCHEME_LABELS = "http://schemas.google.com/g/2005/labels";
663
673
const _GOOGLE_DOCS_TERM_STARRED = "http://schemas.google.com/g/2005/labels#starred";
664
674
 
665
 
function GoogleDocument(cursor) {
666
 
    this._init(cursor);
667
 
}
668
 
 
669
 
GoogleDocument.prototype = {
670
 
    __proto__: DocCommon.prototype,
 
675
const GoogleDocument = new Lang.Class({
 
676
    Name: 'GoogleDocument',
 
677
    Extends: DocCommon,
671
678
 
672
679
    _init: function(cursor) {
673
680
        this._triedThumbnailing = true;
674
681
        this._failedThumbnailing = true;
675
682
 
676
 
        DocCommon.prototype._init.call(this, cursor);
 
683
        this.parent(cursor);
677
684
 
678
685
        // overridden
679
686
        this.defaultAppName = _("Google Docs");
686
693
        let authorizer = new Gd.GDataGoaAuthorizer({ goa_object: source.object });
687
694
        let service = new GData.DocumentsService({ authorizer: authorizer });
688
695
 
689
 
        // HACK: GJS doesn't support introspecting GTypes, so we need to use
690
 
        // GObject.type_from_name(); but for that to work, we need at least one
691
 
        // instance of the GType in question to have ever been created. Ensure that
692
 
        let temp = new GData.DocumentsText();
693
696
        service.query_single_entry_async
694
697
            (service.get_primary_authorization_domain(),
695
698
             this.identifier, null,
696
 
             GObject.type_from_name('GDataDocumentsText'),
 
699
             GData.DocumentsText,
697
700
             cancellable, Lang.bind(this,
698
701
                 function(object, res) {
699
702
                     let entry = null;
718
721
                        function(source, res) {
719
722
                            try {
720
723
                                let document = Gd.pdf_loader_load_uri_finish(res);
721
 
                                callback(this, document, null);
 
724
                                this._finishLoad(document, callback, null);
722
725
                            } catch (e) {
723
726
                                // report the outmost error only
724
 
                                callback(this, null, exception);
725
 
                                return;
 
727
                                this._finishLoad(null, callback, exception);
726
728
                            }
727
729
                        }));
728
730
 
729
731
                    return;
730
732
                }
731
733
 
732
 
                Gd.pdf_loader_load_entry_async
 
734
                Gd.pdf_loader_load_gdata_entry_async
733
735
                    (entry, service, cancellable, Lang.bind(this,
734
736
                        function(source, res) {
735
737
                            try {
736
 
                                let document = Gd.pdf_loader_load_entry_finish(res);
737
 
                                callback(this, document, null);
 
738
                                let document = Gd.pdf_loader_load_uri_finish(res);
 
739
                                this._finishLoad(document, callback, null);
738
740
                            } catch (e) {
739
 
                                callback(this, null, e);
 
741
                                this._finishLoad(null, callback, e);
740
742
                            }
741
743
                        }));
742
744
            }));
760
762
    populateFromCursor: function(cursor) {
761
763
        this.shared = cursor.get_boolean(Query.QueryColumns.SHARED);
762
764
 
763
 
        DocCommon.prototype.populateFromCursor.call(this, cursor);
 
765
        this.parent(cursor);
764
766
    },
765
767
 
766
768
    setFavorite: function(favorite) {
767
 
        DocCommon.prototype.setFavorite.call(this, favorite);
 
769
        this.parent(favorite);
768
770
 
769
771
        this._createGDataEntry(null, Lang.bind(this,
770
772
            function(entry, service, exception) {
806
808
    canTrash: function() {
807
809
        return false;
808
810
    }
809
 
};
810
 
 
811
 
function DocumentManager() {
812
 
    this._init();
813
 
}
814
 
 
815
 
DocumentManager.prototype = {
816
 
    __proto__: Manager.BaseManager.prototype,
 
811
});
 
812
 
 
813
const SkydriveDocument = new Lang.Class({
 
814
    Name: 'SkydriveDocument',
 
815
    Extends: DocCommon,
 
816
 
 
817
    _init: function(cursor) {
 
818
        this._triedThumbnailing = true;
 
819
        this._failedThumbnailing = true;
 
820
 
 
821
        this.parent(cursor);
 
822
 
 
823
        // overridden
 
824
        this.defaultAppName = _("Skydrive");
 
825
        this.sourceName = _("Skydrive");
 
826
    },
 
827
 
 
828
    _createZpjEntry: function(cancellable, callback) {
 
829
        let source = Global.sourceManager.getItemById(this.resourceUrn);
 
830
 
 
831
        let authorizer = new Zpj.GoaAuthorizer({ goa_object: source.object });
 
832
        let service = new Zpj.Skydrive({ authorizer: authorizer });
 
833
 
 
834
        const zpj_prefix = "windows-live:skydrive:";
 
835
        let zpj_id = this.identifier.substring(zpj_prefix.length);
 
836
 
 
837
        service.query_info_from_id_async
 
838
            (zpj_id, cancellable,
 
839
             Lang.bind(this,
 
840
                 function(object, res) {
 
841
                     let entry = null;
 
842
                     let exception = null;
 
843
 
 
844
                     try {
 
845
                         entry = object.query_info_from_id_finish(res);
 
846
                     } catch (e) {
 
847
                         exception = e;
 
848
                     }
 
849
 
 
850
                     callback(entry, service, exception);
 
851
                 }));
 
852
    },
 
853
 
 
854
    load: function(cancellable, callback) {
 
855
        this._createZpjEntry(cancellable, Lang.bind(this,
 
856
            function(entry, service, exception) {
 
857
                if (exception) {
 
858
                    // try loading from the most recent cache, if any
 
859
                    Gd.pdf_loader_load_uri_async(this.identifier, cancellable, Lang.bind(this,
 
860
                        function(source, res) {
 
861
                            try {
 
862
                                let document = Gd.pdf_loader_load_uri_finish(res);
 
863
                                this._finishLoad(document, callback, null);
 
864
                            } catch (e) {
 
865
                                // report the outmost error only
 
866
                                this._finishLoad(null, callback, exception);
 
867
                            }
 
868
                        }));
 
869
 
 
870
                    return;
 
871
                }
 
872
 
 
873
                Gd.pdf_loader_load_zpj_entry_async
 
874
                    (entry, service, cancellable, Lang.bind(this,
 
875
                        function(source, res) {
 
876
                            try {
 
877
                                let document = Gd.pdf_loader_load_zpj_entry_finish(res);
 
878
                                this._finishLoad(document, callback, null);
 
879
                            } catch (e) {
 
880
                                this._finishLoad(null, callback, e);
 
881
                            }
 
882
                        }));
 
883
            }));
 
884
    },
 
885
 
 
886
    updateTypeDescription: function() {
 
887
        let description;
 
888
 
 
889
        if (this.rdfType.indexOf('nfo#Spreadsheet') != -1)
 
890
            description = _("Spreadsheet");
 
891
        else if (this.rdfType.indexOf('nfo#Presentation') != -1)
 
892
            description = _("Presentation");
 
893
        else if (this.rdfType.indexOf('nfo#DataContainer') != -1)
 
894
            description = _("Collection");
 
895
        else
 
896
            description = _("Document");
 
897
 
 
898
        this.typeDescription = description;
 
899
    },
 
900
 
 
901
    setFavorite: function(favorite) {
 
902
    },
 
903
 
 
904
    canTrash: function() {
 
905
        return false;
 
906
    }
 
907
});
 
908
 
 
909
const DocumentManager = new Lang.Class({
 
910
    Name: 'DocumentManager',
 
911
    Extends: Manager.BaseManager,
817
912
 
818
913
    _init: function() {
819
 
        Manager.BaseManager.prototype._init.call(this);
 
914
        this.parent();
820
915
 
821
916
        this._model = new DocumentModel();
822
917
 
867
962
                (identifier.indexOf('https://docs.google.com') != -1));
868
963
    },
869
964
 
 
965
    _identifierIsSkydrive: function(identifier) {
 
966
        return (identifier &&
 
967
                (identifier.indexOf('windows-live:skydrive:') != -1));
 
968
    },
 
969
 
870
970
    createDocumentFromCursor: function(cursor) {
871
971
        let identifier = cursor.get_string(Query.QueryColumns.IDENTIFIER)[0];
872
972
        let doc;
873
973
 
874
974
        if (this._identifierIsGoogle(identifier))
875
975
            doc = new GoogleDocument(cursor);
 
976
        else if (this._identifierIsSkydrive(identifier))
 
977
            doc = new SkydriveDocument(cursor);
876
978
        else
877
979
            doc = new LocalDocument(cursor);
878
980
 
896
998
            items[idx].destroy();
897
999
        };
898
1000
 
899
 
        Manager.BaseManager.prototype.clear.call(this);
 
1001
        this.parent();
900
1002
        this._model.clear();
901
1003
    },
902
1004
 
903
1005
    setActiveItem: function(doc) {
904
 
        if (Manager.BaseManager.prototype.setActiveItem.call(this, doc)) {
905
 
 
906
 
            if (doc && !doc.collection) {
907
 
                let recentManager = Gtk.RecentManager.get_default();
908
 
                recentManager.add_item(doc.uri);
909
 
            }
 
1006
        if (!this.parent(doc))
 
1007
            return;
 
1008
 
 
1009
        if (!doc)
 
1010
            return;
 
1011
 
 
1012
        if (doc.collection) {
 
1013
            Global.collectionManager.setActiveItem(doc);
 
1014
            return;
910
1015
        }
 
1016
 
 
1017
        let recentManager = Gtk.RecentManager.get_default();
 
1018
        recentManager.add_item(doc.uri);
911
1019
    },
912
1020
 
913
1021
    getModel: function() {
914
1022
        return this._model;
915
1023
    }
916
 
};
917
 
 
918
 
function DocumentModel() {
919
 
    this._init();
920
 
}
921
 
 
922
 
DocumentModel.prototype = {
 
1024
});
 
1025
 
 
1026
const DocumentModel = new Lang.Class({
 
1027
    Name: 'DocumentModel',
 
1028
 
923
1029
    _init: function() {
924
 
        this.model = Gd.create_list_store();
 
1030
        this.model = Gtk.ListStore.new(
 
1031
            [ GObject.TYPE_STRING,
 
1032
              GObject.TYPE_STRING,
 
1033
              GObject.TYPE_STRING,
 
1034
              GObject.TYPE_STRING,
 
1035
              GdkPixbuf.Pixbuf,
 
1036
              GObject.TYPE_LONG,
 
1037
              GObject.TYPE_BOOLEAN ]);
925
1038
        this.model.set_sort_column_id(Gd.MainColumns.MTIME,
926
1039
                                      Gtk.SortType.DESCENDING);
927
1040
    },
932
1045
 
933
1046
    documentAdded: function(doc) {
934
1047
        let iter = this.model.append();
935
 
 
936
 
        Gd.store_set(this.model, iter,
937
 
                     doc.id, doc.uri,
938
 
                     doc.name, doc.author,
939
 
                     doc.pixbuf, doc.mtime);
 
1048
        this.model.set(iter,
 
1049
            [ 0, 1, 2, 3, 4, 5 ],
 
1050
            [ doc.id, doc.uri, doc.name,
 
1051
              doc.author, doc.pixbuf, doc.mtime ]);
940
1052
 
941
1053
        let treePath = this.model.get_path(iter);
942
1054
        let treeRowRef = Gtk.TreeRowReference.new(this.model, treePath);
949
1061
 
950
1062
                let objectIter = this.model.get_iter(objectPath)[1];
951
1063
                if (objectIter)
952
 
                    Gd.store_set(this.model, iter,
953
 
                                 doc.id, doc.uri,
954
 
                                 doc.name, doc.author,
955
 
                                 doc.pixbuf, doc.mtime);
 
1064
                    this.model.set(objectIter,
 
1065
                        [ 0, 1, 2, 3, 4, 5 ],
 
1066
                        [ doc.id, doc.uri, doc.name,
 
1067
                          doc.author, doc.pixbuf, doc.mtime ]);
956
1068
            }));
957
1069
    },
958
1070
 
969
1081
                return false;
970
1082
            }));
971
1083
    }
972
 
};
 
1084
});