~ubuntu-branches/ubuntu/quantal/zeitgeist/quantal

« back to all changes in this revision

Viewing changes to extensions/fts.vala

  • Committer: Package Import Robot
  • Author(s): Didier Roche
  • Date: 2011-11-15 11:15:56 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20111115111556-4lmc5wdvjrsdm0ss
Tags: 0.8.99~alpha1-1ubuntu1
Upload to ubuntu the new zeitgeist rewritten in vala

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* fts.vala
 
2
 *
 
3
 * Copyright © 2011 Seif Lotfy <seif@lotfy.com>
 
4
 * Copyright © 2011 Canonical Ltd.
 
5
 *             By Michal Hruby <michal.hruby@canonical.com>
 
6
 *
 
7
 * This program is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU Lesser General Public License as published by
 
9
 * the Free Software Foundation, either version 2.1 of the License, or
 
10
 * (at your option) any later version.
 
11
 *
 
12
 * This program is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 *
 
17
 * You should have received a copy of the GNU Lesser General Public License
 
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 */
 
21
 
 
22
namespace Zeitgeist
 
23
{
 
24
    [DBus (name = "org.gnome.zeitgeist.Index")]
 
25
    public interface RemoteSearchEngine: Object
 
26
    {
 
27
        [DBus (signature = "a(asaasay)u")]
 
28
        public abstract async Variant search (
 
29
            string query_string,
 
30
            [DBus (signature = "(xx)")] Variant time_range,
 
31
            [DBus (signature = "a(asaasay)")] Variant filter_templates,
 
32
            uint offset, uint count, uint result_type,
 
33
            [DBus (signature = "a(asaasay)")] out Variant events) throws Error;
 
34
    }
 
35
 
 
36
    /* Because of a Vala bug we have to define the proxy interface outside of
 
37
     * [ModuleInit] source */
 
38
    /*
 
39
    [DBus (name = "org.gnome.zeitgeist.SimpleIndexer")]
 
40
    public interface RemoteSimpleIndexer : Object
 
41
    {
 
42
        [DBus (signature = "a(asaasay)u")]
 
43
        public abstract async Variant search (
 
44
            string query_string,
 
45
            [DBus (signature = "(xx)")] Variant time_range,
 
46
            [DBus (signature = "a(asaasay)")] Variant filter_templates,
 
47
            uint offset, uint count, uint result_type) throws Error;
 
48
    }
 
49
    */
 
50
 
 
51
    class SearchEngine: Extension, RemoteSearchEngine
 
52
    {
 
53
 
 
54
        private RemoteSimpleIndexer siin;
 
55
        private uint registration_id;
 
56
 
 
57
        SearchEngine ()
 
58
        {
 
59
            Object ();
 
60
        }
 
61
 
 
62
        construct
 
63
        {
 
64
            try
 
65
            {
 
66
                var connection = Bus.get_sync (BusType.SESSION, null);
 
67
                registration_id = connection.register_object<RemoteSearchEngine> (
 
68
                    "/org/gnome/zeitgeist/index/activity", this);
 
69
 
 
70
                // FIXME: shouldn't we delay this to next idle callback?
 
71
                // Get SimpleIndexer
 
72
                Bus.watch_name_on_connection (connection,
 
73
                    "org.gnome.zeitgeist.SimpleIndexer",
 
74
                    BusNameWatcherFlags.AUTO_START,
 
75
                    (conn) =>
 
76
                    {
 
77
                        if (siin != null) return;
 
78
                        conn.get_proxy.begin<RemoteSimpleIndexer> (
 
79
                            "org.gnome.zeitgeist.SimpleIndexer",
 
80
                            "/org/gnome/zeitgeist/index/activity",
 
81
                            0, null, this.proxy_acquired);
 
82
                    },
 
83
                    () => {});
 
84
            }
 
85
            catch (Error err)
 
86
            {
 
87
                warning ("%s", err.message);
 
88
            }
 
89
        }
 
90
 
 
91
        private void proxy_acquired (Object? obj, AsyncResult res)
 
92
        {
 
93
            var conn = obj as DBusConnection;
 
94
            try
 
95
            {
 
96
                siin = conn.get_proxy.end<RemoteSimpleIndexer> (res);
 
97
            }
 
98
            catch (IOError err)
 
99
            {
 
100
                warning ("%s", err.message);
 
101
            }
 
102
        }
 
103
 
 
104
        /* This whole method is one huge workaround for an issue with Vala
 
105
         * enclosing all out/return parameters in a TUPLE variant */
 
106
        public async Variant search (string query_string, Variant time_range,
 
107
            Variant filter_templates, uint offset, uint count, uint result_type,
 
108
            out Variant events) throws Error
 
109
        {
 
110
            debug ("Performing search for %s", query_string);
 
111
            if (siin == null || !(siin is DBusProxy))
 
112
            {
 
113
                // FIXME: queue until we have the proxy
 
114
                throw new EngineError.DATABASE_ERROR (
 
115
                    "Not connected to SimpleIndexer");
 
116
            }
 
117
            var timer = new Timer ();
 
118
            DBusProxy proxy = (DBusProxy) siin;
 
119
            var b = new VariantBuilder (new VariantType ("(s(xx)a(asaasay)uuu)"));
 
120
            b.add ("s", query_string);
 
121
            b.add_value (time_range);
 
122
            b.add_value (filter_templates);
 
123
            b.add ("u", offset);
 
124
            b.add ("u", count);
 
125
            b.add ("u", result_type);
 
126
            var result = yield proxy.call ("Search", b.end (), 0, -1, null);
 
127
            events = result.get_child_value (0);
 
128
            /* FIXME: this somehow doesn't work :(
 
129
             *   but it's fixable in a similar way as this method's signature
 
130
             *   is done */
 
131
            /*
 
132
            var result = yield siin.search (query_string, time_range,
 
133
                filter_templates, offset, count, result_type);
 
134
            */
 
135
            debug ("Got %u results from indexer (in %f seconds)",
 
136
                (uint) events.n_children (), timer.elapsed ());
 
137
            return result.get_child_value (1);
 
138
        }
 
139
 
 
140
    }
 
141
 
 
142
    [ModuleInit]
 
143
#if BUILTIN_EXTENSIONS
 
144
    public static Type fts_init (TypeModule module)
 
145
    {
 
146
#else
 
147
    public static Type extension_register (TypeModule module)
 
148
    {
 
149
#endif
 
150
        return typeof (SearchEngine);
 
151
    }
 
152
}
 
153
 
 
154
// vim:expandtab:ts=4:sw=4