~zeitgeist-dataproviders/zeitgeist-datasources/git

« back to all changes in this revision

Viewing changes to firefox/extension/chrome/content/event.js

  • Committer: Siegfried-Angel Gevatter Pujals
  • Date: 2012-01-30 19:00:44 UTC
  • mto: This revision was merged to the branch mainline in revision 163.
  • Revision ID: git-v1:ee91ed7f63dd392f71bb076fe346162bd93c6c0e
Firefox: log downloads

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
function getOriginFromUri(uri) {
 
2
        var parts = uri.split("://", 2);
 
3
        if (parts[0] == "file") {
 
4
                // Local file URI, we just trim it to directory in which the file is
 
5
                return uri.substring(0, uri.lastIndexOf('/') + 1);
 
6
        } else {
 
7
                // HTTP(S), FTP or any other URI, we only take up to the domain name
 
8
                return parts[0] + "://" + parts[1].split("/")[0] + "/";
 
9
        }
 
10
}
 
11
 
 
12
function getManifestationFromUri(uri) {
 
13
        var parts = uri.split("://", 2);
 
14
        if (parts[0] == "file") {
 
15
                return "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#FileDataObject";
 
16
        } else if (parts[0] == "http" || parts[0] == "https") {
 
17
                return "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#WebDataObject";
 
18
        } else {
 
19
                // May be FTP or something else
 
20
                return "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#RemoteDataObject";
 
21
        }
 
22
}
 
23
 
 
24
/*
 
25
function isDebugEnabled() {
 
26
        var prefs = Components.classes["@mozilla.org/preferences-service;1"]
 
27
                .getService(Components.interfaces.nsIPrefBranch);
 
28
        return prefs.getBoolPref("extensions.zeitgeist.log");
 
29
}
 
30
*/
 
31
 
 
32
function isPrivateBrowsing() {
 
33
        var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
 
34
                .getService(Components.interfaces.nsIPrivateBrowsingService);
 
35
        return pbs.privateBrowsingEnabled;
 
36
}
 
37
 
1
38
var ZeitgeistProgressListener = {
2
39
        onStateChange: function(aBrowser, aProgress, aRequest, aStateFlags) {
3
 
                
4
 
                //Don't do anything if Private Browsing is enabled
5
 
                var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
6
 
                                .getService(Components.interfaces.nsIPrivateBrowsingService);
7
 
                if (pbs.privateBrowsingEnabled) return;   
8
 
                                                
 
40
                // Don't do anything if Private Browsing is enabled
 
41
                if (isPrivateBrowsing()) return;
 
42
        
9
43
                if (aStateFlags & Components.interfaces.nsIWebProgressListener.STATE_STOP) {
10
44
                        let uri = aBrowser.currentURI.spec;
11
 
                        //Ignore pages without titles (generally redirect pages)
 
45
                        // Ignore pages without titles (generally redirect pages)
12
46
                        if (aBrowser.contentTitle == "") return;
13
47
                        
14
48
                        let mimetype = aBrowser.contentDocument.contentType;
15
49
                        if (aRequest.name == uri && !this.ignore_uri(uri)) {
16
 
                                var origin;
17
 
                                var parts = uri.split("://", 2);
18
 
                                if (parts[0] == "file") {
19
 
                                        // Local file URI, we just trim it to directory in which the file is
20
 
                                        origin = uri.substring(0, uri.lastIndexOf('/') + 1);
21
 
                                } else {
22
 
                                        // HTTP(S), FTP or any other URI, we only take up to the domain name
23
 
                                        origin = parts[0] + "://" + parts[1].split("/")[0] + "/";
24
 
                                }
25
 
                                                
26
 
                                let subject = libzeitgeist.zeitgeist_subject_new_full(  uri,
 
50
                                let origin = getOriginFromUri(uri);
 
51
                                let subject_manifestation = getManifestationFromUri(uri);
 
52
 
 
53
                                let subject = libzeitgeist.zeitgeist_subject_new_full(uri,
27
54
                                                                                                                                        "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Website",
28
 
                                                                                                                                        "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#RemoteDataObject",
 
55
                                                                                                                                        subject_manifestation,
29
56
                                                                                                                                        mimetype,
30
57
                                                                                                                                        origin,
31
58
                                                                                                                                        aBrowser.contentTitle,
38
65
                                                                                                                                        null);
39
66
                                                                
40
67
                                libzeitgeist.zeitgeist_log_insert_events_no_reply(libzeitgeist.log, event, null);
41
 
                                
42
 
                                //Log event in Firefox's error console if logging pref is true
43
 
                                var prefs = Components.classes["@mozilla.org/preferences-service;1"]
44
 
                                                                                .getService(Components.interfaces.nsIPrefBranch);
45
 
                                if (prefs.getBoolPref("extensions.zeitgeist.log")) {
 
68
 
 
69
                                /*      
 
70
                                if (isDebugEnabled()) {
46
71
                                        zeitgeist.debug("Event added to zeitgeist:" +
47
72
                                                                        "\n\t\tevent interpretation: EVENT_INTERPRETATION.ACCESS_EVENT" +
48
73
                                                                        "\n\t\tEvent manifestation: EVENT_MANIFESTATION.USER_ACTIVITY" +
55
80
                                                                        "\n\t\t\ttitle: " + aBrowser.contentTitle +
56
81
                                                                        "\n\t\t\tstorage: net");
57
82
                                }
 
83
                                */
58
84
                                
59
85
                                var googlemail_view_regex = new RegExp("mail\\.google\\.com");
60
86
                                if (ZeitgeistPrefObserver.get_bool("enable_googlemail") & googlemail_view_regex.test(uri)) {
89
115
        onSecurityChange: function(){},
90
116
        onProgressChange: function(){},
91
117
        
92
 
        //Useful helpers        
 
118
        // Useful helpers       
93
119
        ignore_uri: function(uri) {
94
120
                for (pattern in ZeitgeistPrefObserver.ignored_uris) {
95
121
                        if (ZeitgeistPrefObserver.ignored_uris[pattern].test(uri)) {
100
126
        },
101
127
};
102
128
 
 
129
var ZeitgeistDownloadManagerListener = {
 
130
        onDownloadStateChange: function(state, dl) {
 
131
                // FIXME: DOWNLOAD_FINISHED would probably be more awesome, but
 
132
                //        it doesn't seem to be sent correctly :(
 
133
                if (state != 0 /*DOWNLOAD_DOWNLOADING*/) return;
 
134
                if (isPrivateBrowsing()) return;
 
135
 
 
136
                let uri = 'file://' + dl.targetFile.path;
 
137
                let subject = libzeitgeist.zeitgeist_subject_new_full(
 
138
                        uri,
 
139
                        "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Website",
 
140
                        "", // empty manifestation, Bluebird Alpha 2+ will figure it out
 
141
                        "", // FIXME: not working: dl.MIMEInfo.type
 
142
                        getOriginFromUri(uri),
 
143
                        dl.displayName,
 
144
                        "" // figure our storage (in case of saving to usb/sftp/etc)
 
145
                );
 
146
                
 
147
                // FIXME: libzeitgeist is missing event origin!
 
148
                // FIXME: where do we put the original download URI? event origin
 
149
                //        is already supposed to be the page where you clicked
 
150
                //        the download link...
 
151
                let event_ = libzeitgeist.zeitgeist_event_new_full(
 
152
                        "http://www.zeitgeist-project.com/ontologies/2010/01/27/zg#CreateEvent", 
 
153
                        "http://www.zeitgeist-project.com/ontologies/2010/01/27/zg#UserActivity", 
 
154
                        "application://firefox.desktop",
 
155
                        subject,
 
156
                        null
 
157
                );
 
158
 
 
159
                libzeitgeist.zeitgeist_log_insert_events_no_reply(libzeitgeist.log, event_, null);
 
160
        },
 
161
 
 
162
        onSecurityChange: function() {},
 
163
        onProgressChange: function() {},
 
164
        onStateChange: function() {}
 
165
};
 
166
 
103
167
var zeitgeist = {
104
168
        init: function() { 
105
169
                ZeitgeistPrefObserver.register();
 
170
 
 
171
                // Listen to tab changes
106
172
                gBrowser.addTabsProgressListener(ZeitgeistProgressListener);
 
173
 
 
174
                // Listen to downloads
 
175
                var downloadManager = Components.classes["@mozilla.org/download-manager;1"]
 
176
                        .getService(Components.interfaces.nsIDownloadManager);
 
177
                downloadManager.addListener(ZeitgeistDownloadManagerListener);
 
178
 
107
179
                libzeitgeist.init();
108
180
        },
109
181