summaryrefslogtreecommitdiff
path: root/libzeitgeist/subject.vala
blob: b6536b861a1cf34e1fd58caa6db99492a3e74988 (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
/* subject.vala
 *
 * Copyright © 2011-2012 Collabora Ltd.
 *             By Seif Lotfy <seif@lotfy.com>
 *             By Siegfried-Angel Gevatter Pujals <siegfried@gevatter.com>
 * Copyright © 2011 Manish Sinha <manishsinha@ubuntu.com>
 * Copyright © 2011 Michal Hruby <michal.mhr@gmail.com>
 * Copyright © 2012 Canonical Ltd.
 *             By Siegfried-A. Gevatter <siegfried.gevatter@collabora.co.uk>
 *
 * 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 2.1 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 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/>.
 *
 */

using Zeitgeist;

namespace Zeitgeist
{

/**
 * Subject objects abstract Zeitgeist subjects
 *
 * In Zeitgeist terminology, a //subject// is something (a file, web page,
 * person, conversation, etc.) that was somehow involved or affected by
 * a {@link Event}.
 */
public class Subject : Object
{
    private static StringChunk url_store;

    public string? uri { get; set; }
    public string? origin { get; set; }
    public string? text { get; set; }
    public string? storage { get; set; }
    // FIXME: current_{uri,origin} are often the same as uri, we don't
    // need to waste memory for them
    public string? current_uri { get; set; }
    public string? current_origin { get; set; }

    public string? mimetype
    {
        get { return _mimetype; }
        set { _mimetype = (value != null) ? url_store.insert_const (value) : null; }
    }
    public string? interpretation
    {
        get { return _interpretation; }
        set { _interpretation = (value != null) ? url_store.insert_const (value) : null; }
    }
    public string? manifestation
    {
        get { return _manifestation; }
        set { _manifestation = (value != null) ? url_store.insert_const (value) : null; }
    }

    private unowned string? _mimetype;
    private unowned string? _interpretation;
    private unowned string? _manifestation;

    static construct
    {
        url_store = new StringChunk (4096);
    }

    public Subject.full (string? uri=null,
        string? interpretation=null, string? manifestation=null,
        string? mimetype=null, string? origin=null, string? text=null,
        string? storage=null)
    {
        this.uri = uri;
        this.interpretation = interpretation;
        this.manifestation = manifestation;
        this.mimetype = mimetype;
        this.origin = origin;
        this.text = text;
        this.storage = storage;
    }

    public Subject.move_event (
        string? source_uri=null, string? source_origin=null,
        string? destination_uri=null, string? destination_origin=null,
        string? interpretation, string? manifestation=null,
        string? mimetype=null, string? text=null, string? storage=null)
    {
        this.uri = source_uri;
        this.origin = source_origin;
        this.current_uri = destination_uri;
        this.current_origin = destination_origin;
        this.interpretation = interpretation;
        this.manifestation = manifestation;
        this.mimetype = mimetype;
        this.text = text;
        this.storage = storage;
    }

    public Subject.from_variant (Variant subject_variant)
        throws DataModelError
    {
        VariantIter iter = subject_variant.iterator();

        var subject_props = iter.n_children ();
        Utils.assert_sig (subject_props >= 7, "Missing subject information");
        uri = next_string_or_null (iter);
        interpretation = next_string_or_null (iter);
        manifestation = next_string_or_null (iter);
        origin = next_string_or_null (iter);
        mimetype = next_string_or_null (iter);
        text = next_string_or_null (iter);
        storage = next_string_or_null (iter);
        // let's keep this compatible with older clients
        if (subject_props >= 8)
            current_uri = next_string_or_null (iter);
        if (subject_props >= 9)
            current_origin = next_string_or_null (iter);
    }

    public Variant to_variant ()
    {
        /* The FAST version */
        char* ptr_arr[9];
        ptr_arr[0] = uri != null ? uri : "";
        ptr_arr[1] = interpretation != null ? interpretation : "";
        ptr_arr[2] = manifestation != null ? manifestation : "";
        ptr_arr[3] = origin != null ? origin : "";
        ptr_arr[4] = mimetype != null ? mimetype : "";
        ptr_arr[5] = text != null ? text : "";
        ptr_arr[6] = storage != null ? storage : "";
        ptr_arr[7] = current_uri != null ? current_uri : "";
        ptr_arr[8] = current_origin != null ? current_origin : "";
        return new Variant.strv ((string[]) ptr_arr);
        /* The NICE version */
        /*
        var vb = new VariantBuilder (new VariantType ("as"));
        vb.add ("s", uri ?? "");
        vb.add ("s", interpretation ?? "");
        vb.add ("s", manifestation ?? "");
        vb.add ("s", origin ?? "");
        vb.add ("s", mimetype ?? "");
        vb.add ("s", text ?? "");
        vb.add ("s", storage ?? "");
        vb.add ("s", current_uri ?? "");
        vb.add ("s", current_origin ?? "");

        return vb.end ();
        */
    }

    public bool matches_template (Subject template_subject)
    {
        /**
        Return True if this Subject matches *subject_template*. Empty
        fields in the template are treated as wildcards.
        Interpretations and manifestations are also matched if they are
        children of the types specified in `subject_template`.
        */
        if (!check_field_match (this.uri, template_subject.uri, false, true))
            return false;
        if (!check_field_match (this.current_uri, template_subject.current_uri, false, true))
            return false;
        if (!check_field_match (this.interpretation, template_subject.interpretation, true))
            return false;
        if (!check_field_match (this.manifestation, template_subject.manifestation, true))
            return false;
        if (!check_field_match (this.origin, template_subject.origin, false, true))
            return false;
        if (!check_field_match (this.current_origin, template_subject.current_origin, false, true))
            return false;
        if (!check_field_match (this.mimetype, template_subject.mimetype, false, true))
            return false;

        return true;
    }

}

}

// vim:expandtab:ts=4:sw=4