~zeitgeist/zeitgeist/saucy-packaging-0-9-14

« back to all changes in this revision

Viewing changes to extensions/histogram.vala

  • Committer: Package Import Robot
  • Author(s): Didier Roche, 11.04 → 12.04 upgrade
  • Date: 2012-05-03 14:28:29 UTC
  • Revision ID: package-import@ubuntu.com-20120503142829-79ij3euvsxl8icuy
Tags: 0.9.0-1ubuntu1
* debian/patches/01_upstream_cherry_picks.patch:
  - picks some upstream fixes:
  zeitgeist-fts crashed with SIGSEGV in fast_validate()
  (LP: #981300, #954171)
  [11.04 → 12.04 upgrade] Dash is always empty due to zeitgeist crash:
  Unable to upgrade from schema version 3 (LP: #986191)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* histogram.vala
 
2
 *
 
3
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 
4
 * Copyright © 2011 Stefano Candori <stefano.candori@gmail.com>
 
5
 *
 
6
 * Based upon a Python implementation (2010-2011) by:
 
7
 *  Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 
8
 *
 
9
 * This program is free software: you can redistribute it and/or modify
 
10
 * it under the terms of the GNU Lesser General Public License as published by
 
11
 * the Free Software Foundation, either version 2.1 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU Lesser General Public License
 
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 *
 
22
 */
 
23
 
 
24
namespace Zeitgeist
 
25
{
 
26
    [DBus (name = "org.gnome.zeitgeist.Histogram")]
 
27
    public interface RemoteHistogram: Object
 
28
    {
 
29
        [DBus (signature = "a(xu)")]
 
30
        public abstract Variant get_histogram_data () throws Error;
 
31
    }
 
32
 
 
33
    class Histogram: Extension, RemoteHistogram
 
34
    {
 
35
 
 
36
        private uint registration_id = 0;
 
37
 
 
38
        construct
 
39
        {
 
40
            // This will be called after bus is acquired, so it shouldn't block
 
41
            try
 
42
            {
 
43
                var connection = Bus.get_sync (BusType.SESSION, null);
 
44
                registration_id = connection.register_object<RemoteHistogram> (
 
45
                    "/org/gnome/zeitgeist/journal/activity", this);
 
46
            }
 
47
            catch (Error err)
 
48
            {
 
49
                warning ("%s", err.message);
 
50
            }
 
51
        }
 
52
 
 
53
        public override void unload ()
 
54
        {
 
55
            try
 
56
            {
 
57
                var connection = Bus.get_sync (BusType.SESSION, null);
 
58
                if (registration_id != 0)
 
59
                {
 
60
                    connection.unregister_object (registration_id);
 
61
                    registration_id = 0;
 
62
                }
 
63
            }
 
64
            catch (Error err)
 
65
            {
 
66
                warning ("%s", err.message);
 
67
            }
 
68
 
 
69
            debug ("%s, this.ref_count = %u", Log.METHOD, this.ref_count);
 
70
        }
 
71
 
 
72
        public Variant get_histogram_data () throws Error
 
73
        {
 
74
            var builder = new VariantBuilder (new VariantType ("a(xu)"));
 
75
 
 
76
            string sql = """
 
77
                SELECT strftime('%s', datetime(timestamp/1000, 'unixepoch',
 
78
                'localtime'), 'start of day') AS daystamp,
 
79
                COUNT(*)
 
80
                FROM event
 
81
                GROUP BY daystamp
 
82
                ORDER BY daystamp DESC
 
83
                """;
 
84
 
 
85
            Sqlite.Statement stmt;
 
86
            var database = engine.database;
 
87
            unowned Sqlite.Database db = database.database;
 
88
 
 
89
            int rc = db.prepare_v2 (sql, -1, out stmt);
 
90
            database.assert_query_success (rc, "SQL error");
 
91
 
 
92
            while ((rc = stmt.step ()) == Sqlite.ROW)
 
93
            {
 
94
                int64 t = stmt.column_int64 (0);
 
95
                uint32 count = stmt.column_int (1);
 
96
 
 
97
                builder.add ("(xu)", t, count);
 
98
            }
 
99
            database.assert_query_success (rc, "Error in get_histogram_data",
 
100
                Sqlite.DONE);
 
101
 
 
102
            return builder.end ();
 
103
        }
 
104
 
 
105
    }
 
106
 
 
107
    [ModuleInit]
 
108
#if BUILTIN_EXTENSIONS
 
109
    public static Type histogram_init (TypeModule module)
 
110
    {
 
111
#else
 
112
    public static Type extension_register (TypeModule module)
 
113
    {
 
114
#endif
 
115
        return typeof (Histogram);
 
116
    }
 
117
}
 
118
 
 
119
// vim:expandtab:ts=4:sw=4