summaryrefslogtreecommitdiff
path: root/src/recent-manager-provider.vala
blob: 46aeb24e826e58ea8a8d1c1d0d659f48538e8906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
 * Zeitgeist
 *
 * Copyright (C) 2010 Michal Hruby <michal.mhr@gmail.com>
 * Copyright (C) 2012 Canonical Ltd.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by Michal Hruby <michal.mhr@gmail.com>
 * Authored by Siegfried-A. Gevatter <siegfried.gevatter@collabora.co.uk>
 *
 */

using Zeitgeist;

public class RecentManagerGtk : DataProvider
{
  public RecentManagerGtk (DataHub datahub)
  {
    GLib.Object (unique_id: "com.zeitgeist-project,datahub,recent",
                 name: "Recently Used Documents",
                 description: "Logs events from GtkRecentlyUsed",
                 datahub: datahub);
  }

  // if vala didn't have bug in construct-only properties, the properties
  // would be construct-only
  public override string unique_id { get; construct set; }
  public override string name { get; construct set; }
  public override string description { get; construct set; }

  public override DataHub datahub { get; construct set; }
  public override bool enabled { get; set; default = true; }
  public override bool register { get; construct set; default = true; }

  private unowned Gtk.RecentManager recent_manager;
  private uint idle_id = 0;

  construct
  {
    recent_manager = Gtk.RecentManager.get_default ();
    recent_manager.set_limit (-1);
  }

  public override void start ()
  {
    recent_manager.changed.connect (this.items_changed);

    items_available (get_items ());
  }

  public override void stop ()
  {
    recent_manager.changed.disconnect (this.items_changed);
  }

  private void items_changed ()
  {
    if (idle_id == 0)
    {
      idle_id = Idle.add (() =>
      {
        items_available (get_items ());
        idle_id = 0;
        return false;
      });
    }
  }

  protected GenericArray<Event> get_items ()
  {
    GenericArray<Event> events = new GenericArray<Event> ();

    int64 signal_time = Timestamp.now ();
    string[] ignored_actors = datahub.get_data_source_actors ();

    foreach (Gtk.RecentInfo ri in recent_manager.get_items ())
    {
      // GFile and GtkRecentInfo use different encoding of the uris, so we'll
      // do this
      File file_obj = File.new_for_uri (ri.get_uri ());
      string uri = file_obj.get_uri ();
      if (ri.get_private_hint () || uri.has_prefix ("file:///tmp/"))
        continue;
      if (ri.is_local () && !ri.exists ())
        continue;

      var last_app = ri.last_application ().strip ();
      unowned string exec_str;
      uint count;
      ulong time_;
      bool registered = ri.get_application_info (last_app, out exec_str,
                                                 out count, out time_);
      if (!registered)
      {
        warning ("%s was not registered in RecentInfo item %p", last_app, ri);
        continue;
      }

      string[] exec = exec_str.split_set (" \t\n", 2);

      string? desktop_file;
      if (exec[0] == "soffice" || exec[0] == "ooffice")
      {
        // special case OpenOffice... since it must do everything differently
        desktop_file = Utils.get_ooo_desktop_file_for_mimetype (ri.get_mime_type ());
      }
      else
      {
        desktop_file = Utils.find_desktop_file_for_app (exec[0]);

        // Thunderbird also likes doing funny stuff...
        if (desktop_file == null && exec[0].has_suffix ("-bin"))
        {
          desktop_file = Utils.find_desktop_file_for_app (
            exec[0].substring(0, exec[0].length - 4));
        }
      }

      if (desktop_file == null)
      {
        warning ("Desktop file for \"%s\" was not found, exec: %s, mime_type: %s",
                 uri, exec[0], ri.get_mime_type ());
        continue; // this makes us sad panda
      }

      var actor = "application://%s".printf (Path.get_basename (desktop_file));
      if (actor in ignored_actors)
      {
        continue;
      }

      var parent_file = file_obj.get_parent ();
      string origin = parent_file != null ?
        parent_file.get_uri () : Path.get_dirname (uri);
      var subject =
        new Subject.full (uri,
                          interpretation_for_mimetype (ri.get_mime_type ()),
                          manifestation_for_uri (uri),
                          ri.get_mime_type (),
                          origin,
                          ri.get_display_name (),
                          ""); // FIXME: storage?!

      Event event;
      int64 timestamp;

      // Zeitgeist checks for duplicated events, so we can just inserted
      // all events every time.
      bool log_create = true;
      bool log_modify = true;
      bool log_access = true;

      // However, we don't really want duplicate events with the same
      // timestamp but different interpretations...
      if (ri.get_added () == ri.get_modified ())
      {
        // Creation also changes modified (and visited). If they are the
        // same, we only log the former.
        log_modify = false;
      }
      if (ri.get_modified () == ri.get_visited ())
      {
        // Modification also updated visited. If they are the same, we
        // only log the former.
        log_access = false;
      }

      if (log_create)
      {
        event = new Event.full (ZG.CREATE_EVENT,
                                ZG.USER_ACTIVITY,
                                actor,
                                null, null); //TODO: remove line below when bug is fixed
        event.add_subject (subject);
        timestamp = ri.get_added ();
        timestamp *= 1000;
        event.timestamp = timestamp;
        if (timestamp > last_timestamp && timestamp >= 0)
        {
          events.add ((owned) event);
        }
      }

      if (log_modify)
      {
        event = new Event.full (ZG.MODIFY_EVENT,
                                ZG.USER_ACTIVITY,
                                actor,
                                null, null); //TODO: remove line below when bug is fixed
        event.add_subject (subject);
        timestamp = ri.get_modified ();
        timestamp *= 1000;
        event.timestamp = timestamp;
        if (timestamp > last_timestamp && timestamp >= 0)
        {
          events.add ((owned) event);
        }
      }

      if (log_access)
      {
        event = new Event.full (ZG.ACCESS_EVENT,
                                ZG.USER_ACTIVITY,
                                actor,
                                null, null); //TODO: remove line below when bug is fixed
        event.add_subject (subject);
        timestamp = ri.get_visited ();
        timestamp *= 1000;
        event.timestamp = timestamp;
        if (timestamp > last_timestamp && timestamp >= 0)
        {
          events.add ((owned) event);
        }
      }

    }

    last_timestamp = signal_time;

    return events;
  }
}