File: kde-recent-document-provider.vala

package info (click to toggle)
zeitgeist 0.9.14-2.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 10,500 kB
  • ctags: 4,031
  • sloc: ansic: 16,835; sh: 11,484; xml: 8,822; python: 8,196; cpp: 2,970; sql: 1,192; makefile: 941
file content (273 lines) | stat: -rw-r--r-- 9,128 bytes parent folder | download | duplicates (5)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
 * 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 RecentDocumentsKDE : DataProvider
{
  public RecentDocumentsKDE (DataHub datahub) throws GLib.Error
  {
    GLib.Object (unique_id: "com.zeitgeist-project,datahub,kde-recent",
                 name: "Recently Used Documents (KDE)",
                 description: "Logs events from KRecentDocument",
                 datahub: datahub);
  }

  private const string RECENT_DOCUMENTS_PATH =
    "/.kde/share/apps/RecentDocuments";
  private const string RECENT_FILE_GROUP = "Desktop Entry";

  private const string ATTRIBUTE_SEPARATOR = ",";
  private const string FILE_ATTRIBUTE_QUERY_RECENT =
    GLib.FileAttribute.STANDARD_TYPE + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_MODIFIED + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_MODIFIED_USEC;
  private const string FILE_ATTRIBUTE_QUERY_SUBJECT =
    GLib.FileAttribute.STANDARD_CONTENT_TYPE + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_MODIFIED + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_MODIFIED_USEC + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_CHANGED + ATTRIBUTE_SEPARATOR +
    GLib.FileAttribute.TIME_CHANGED_USEC;

  private const int TIME_EPSILON = 100; // msec

  // 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 string recent_document_path;
  private GLib.File recent_documents_directory;
  private GLib.FileMonitor monitor;
  private string[] ignored_actors;

  private GLib.Regex recent_regex;
  private GLib.Regex url_regex;
  private const string RECENT_REGEX_REPLACEMENT = "URL=";

  construct
  {
    //FIXME: is done properly ?
    try
    {
      recent_regex = new Regex ("URL\\[[^]]+\\]=");
      url_regex = new Regex ("\\$HOME");
    }
    catch (RegexError err)
    {
      warning ("Couldn't process regex: %s", err.message);
    }
    recent_document_path = Environment.get_home_dir () + RECENT_DOCUMENTS_PATH;
    recent_documents_directory = File.new_for_path (recent_document_path);
    try
    {
      monitor = recent_documents_directory.monitor_directory (
          GLib.FileMonitorFlags.NONE);
    }
    catch (GLib.IOError err)
    {
      warning ("Couldn't set up monitor: %s", err.message);
    }
  }

  public override void start ()
  {
    ignored_actors = datahub.get_data_source_actors ();
    monitor.changed.connect (this.process_event);

    crawl_all_items.begin ();
  }

  public override void stop ()
  {
    monitor.changed.disconnect (this.process_event);
  }

  private async void process_event (GLib.File file, GLib.File? other_file,
    GLib.FileMonitorEvent event_type)
  {
    if (event_type == GLib.FileMonitorEvent.CREATED ||
        event_type == GLib.FileMonitorEvent.CHANGED ||
        event_type == GLib.FileMonitorEvent.ATTRIBUTE_CHANGED)
    {
      try
      {
        Event? event = yield parse_file (file);
        if (event != null)
        {
          GenericArray<Event> events = new GenericArray<Event> ();
          events.add ((owned) event);
          items_available (events);
        }
      }
      catch (GLib.Error err)
      {
        warning ("Couldn't process %s: %s", file.get_path (), err.message);
      }
    }
  }

  private async Event? parse_file (GLib.File file) throws GLib.Error
  {
    TimeVal timeval;

    if (!file.get_basename ().has_suffix (".desktop"))
      return null;

    var recent_info = yield file.query_info_async (
      FILE_ATTRIBUTE_QUERY_RECENT, GLib.FileQueryInfoFlags.NONE);

    GLib.FileType file_type = (GLib.FileType) recent_info.get_attribute_uint32 (
      GLib.FileAttribute.STANDARD_TYPE);
    if (file_type != GLib.FileType.REGULAR)
      return null;

    timeval = recent_info.get_modification_time ();
    int64 event_time = Timestamp.from_timeval (timeval);

    string? content = Utils.get_file_contents (file);
    if (content == null)
      return null;
    content = recent_regex.replace (content, content.length, 0,
      RECENT_REGEX_REPLACEMENT);

    KeyFile recent_file = new KeyFile ();
    recent_file.load_from_data (content, content.length, KeyFileFlags.NONE);
    string basename = recent_file.get_string (RECENT_FILE_GROUP, "Name");
    string uri = recent_file.get_string (RECENT_FILE_GROUP, "URL");
    string desktop_entry_name = recent_file.get_string (RECENT_FILE_GROUP,
      "X-KDE-LastOpenedWith");

    // URL may contain environment variables. In practice, KConfigGroup
    // only uses $HOME.
    uri = url_regex.replace (uri, uri.length, 0, Environment.get_home_dir ());

    string? actor = get_actor_for_desktop_entry_name (desktop_entry_name);
    if (actor == null)
    {
        warning ("Couldn't find actor for '%s'.", desktop_entry_name);
        return null;
    }
    if (actor in ignored_actors)
      return null;

    GLib.File subject_file = File.new_for_uri (uri);
    var subject_info = subject_file.query_info (
      FILE_ATTRIBUTE_QUERY_SUBJECT, GLib.FileQueryInfoFlags.NONE);

    timeval = subject_info.get_modification_time ();
    int64 modification_time = Timestamp.from_timeval (timeval);

    timeval.tv_sec = (long) subject_info.get_attribute_uint64 (
      GLib.FileAttribute.TIME_CHANGED);
    timeval.tv_usec = subject_info.get_attribute_uint32 (
      GLib.FileAttribute.TIME_CHANGED_USEC);
    int64 creation_time = Timestamp.from_timeval (timeval);

    string mimetype = subject_info.get_attribute_string (
      FileAttribute.STANDARD_CONTENT_TYPE);

    string event_interpretation;
    int64 creation_diff = event_time - creation_time;
    int64 modification_diff = event_time - modification_time;
    if (creation_diff.abs () < TIME_EPSILON)
      event_interpretation = ZG.CREATE_EVENT;
    else if (modification_diff.abs () < TIME_EPSILON)
      event_interpretation = ZG.MODIFY_EVENT;
    else
      event_interpretation = ZG.ACCESS_EVENT;

    string origin = Path.get_dirname (uri);
    var subject =
      new Subject.full (uri,
                        interpretation_for_mimetype (mimetype),
                        manifestation_for_uri (uri),
                        mimetype,
                        origin,
                        basename,
                        ""); // storage will be figured out by Zeitgeist

    Event event = new Event.full (event_interpretation, ZG.USER_ACTIVITY,
                                  actor, null, null);
    event.add_subject (subject);
    event.timestamp = event_time;

    return event;
  }

  private string? get_actor_for_desktop_entry_name (string desktop_entry_name)
  {
      const string desktop_prefixes[] = { "", "kde-", "kde4-" };

      DesktopAppInfo dae = null;
      string desktop_id = null;
      foreach (unowned string prefix in desktop_prefixes)
      {
          desktop_id = "%s%s.desktop".printf (prefix, desktop_entry_name);
          dae = new DesktopAppInfo (desktop_id);
          if (dae != null)
            break;
      }

      if (dae != null)
      {
          return "application://%s".printf (desktop_id);
      }

      return null;
  }

  private async void crawl_all_items () throws GLib.Error
  {
    GenericArray<Event> events = new GenericArray<Event> ();

    GLib.File directory = GLib.File.new_for_path (recent_document_path);
    GLib.FileEnumerator enumerator = directory.enumerate_children (
      FileAttribute.STANDARD_NAME, GLib.FileQueryInfoFlags.NONE);
    GLib.FileInfo fi;
    while ((fi = enumerator.next_file ()) != null)
    {
      var file = directory.get_child (fi.get_name ());
      try
      {
        Event? event = yield parse_file (file);
        if (event != null)
          events.add ((owned) event);
      }
      catch (GLib.Error err)
      {
        // Silently ignore. The files may be gone by now - who cares?
      }
    }
    enumerator.close ();

    // Zeitgeist will take care of ignoring the duplicates
    items_available (events);
  }
}