1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  *  Copyright (C) 2005 Takuro Ashie <ashie@homa.ne.jp>
4  *  Copyright (C) 2006 Juernjakob Harder <juernjakob.harder@gmail.com>
5  *
6  *  This library is free software; you can redistribute it and/or
7  *  modify it under the terms of the GNU Lesser General Public
8  *  License as published by the Free Software Foundation; either
9  *  version 2 of the License, or (at your option) any later version.
10  *
11  *  This library is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU Lesser General Public License for more details.
15  *
16  *  You should have received a copy of the GNU Lesser General Public
17  *  License along with this program; if not, write to the
18  *  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19  *  Boston, MA  02111-1307  USA
20  *
21  *  $Id: tomoe-candidate.c 1307 2007-06-07 03:31:25Z ikezoe $
22  */
23 
24 #include "tomoe-candidate.h"
25 
26 enum {
27     PROP_0,
28     PROP_CHAR,
29     PROP_SCORE
30 };
31 
32 #define TOMOE_CANDIDATE_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), TOMOE_TYPE_CANDIDATE, TomoeCandidatePrivate))
33 
34 typedef struct _TomoeCandidatePrivate   TomoeCandidatePrivate;
35 struct _TomoeCandidatePrivate
36 {
37     TomoeChar    *character;
38     gint          score;
39 };
40 
41 G_DEFINE_TYPE (TomoeCandidate, tomoe_candidate, G_TYPE_OBJECT)
42 
43 static void dispose      (GObject      *object);
44 static void set_property (GObject      *object,
45                           guint         prop_id,
46                           const GValue *value,
47                           GParamSpec   *pspec);
48 static void get_property (GObject      *object,
49                           guint         prop_id,
50                           GValue       *value,
51                           GParamSpec   *pspec);
52 
53 static void
tomoe_candidate_class_init(TomoeCandidateClass * klass)54 tomoe_candidate_class_init (TomoeCandidateClass *klass)
55 {
56     GObjectClass *gobject_class;
57 
58     gobject_class = G_OBJECT_CLASS (klass);
59 
60     gobject_class->dispose      = dispose;
61     gobject_class->set_property = set_property;
62     gobject_class->get_property = get_property;
63 
64     g_object_class_install_property (
65         gobject_class,
66         PROP_CHAR,
67         g_param_spec_object (
68             "char",
69             "Character",
70             "A TomoeChar object",
71             TOMOE_TYPE_CHAR,
72             G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
73     g_object_class_install_property(
74         gobject_class,
75         PROP_SCORE,
76         g_param_spec_uint(
77             "score",
78             "Score",
79             "Score of this candidate. Lower value has higher priority.",
80             0,
81             G_MAXINT,
82             0,
83             G_PARAM_READWRITE));
84 
85 
86     g_type_class_add_private (gobject_class, sizeof (TomoeCandidatePrivate));
87 }
88 
89 static void
tomoe_candidate_init(TomoeCandidate * cand)90 tomoe_candidate_init (TomoeCandidate *cand)
91 {
92     TomoeCandidatePrivate *priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
93 
94     priv->character = NULL;
95     priv->score     = 0;
96 }
97 
98 static void
dispose(GObject * object)99 dispose (GObject *object)
100 {
101     TomoeCandidatePrivate *priv = TOMOE_CANDIDATE_GET_PRIVATE (object);
102 
103     if (priv->character)
104         g_object_unref (G_OBJECT (priv->character));
105     priv->character = NULL;
106 
107     G_OBJECT_CLASS (tomoe_candidate_parent_class)->dispose (object);
108 }
109 
110 static void
set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)111 set_property (GObject *object,
112               guint prop_id,
113               const GValue *value,
114               GParamSpec *pspec)
115 {
116     TomoeCandidate *cand = TOMOE_CANDIDATE (object);
117     TomoeCandidatePrivate *priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
118 
119     switch (prop_id) {
120     case PROP_CHAR:
121     {
122         GObject *obj = g_value_get_object (value);
123         priv->character = TOMOE_CHAR (g_object_ref (obj));
124         break;
125     }
126     case PROP_SCORE:
127         priv->score = g_value_get_int (value);
128         break;
129     default:
130         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
131         break;
132     }
133 }
134 
135 
136 static void
get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)137 get_property (GObject *object,
138               guint prop_id,
139               GValue *value,
140               GParamSpec *pspec)
141 {
142     TomoeCandidate *cand = TOMOE_CANDIDATE (object);
143     TomoeCandidatePrivate *priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
144 
145     switch (prop_id) {
146     case PROP_CHAR:
147         g_value_set_object (value, G_OBJECT (priv->character));
148         break;
149     case PROP_SCORE:
150         g_value_set_int (value, priv->score);
151         break;
152     default:
153         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
154         break;
155     }
156 }
157 
158 TomoeCandidate*
tomoe_candidate_new(TomoeChar * chr)159 tomoe_candidate_new (TomoeChar *chr)
160 {
161     TomoeCandidate *cand;
162 
163     cand = g_object_new(TOMOE_TYPE_CANDIDATE,
164                         "char", chr,
165                         NULL);
166 
167     return cand;
168 }
169 
170 TomoeChar *
tomoe_candidate_get_char(TomoeCandidate * cand)171 tomoe_candidate_get_char (TomoeCandidate *cand)
172 {
173     TomoeCandidatePrivate *priv;
174 
175     g_return_val_if_fail (TOMOE_IS_CANDIDATE (cand), NULL);
176 
177     priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
178     g_return_val_if_fail (priv, NULL);
179 
180     return priv->character;
181 }
182 
183 void
tomoe_candidate_set_score(TomoeCandidate * cand,gint score)184 tomoe_candidate_set_score (TomoeCandidate *cand, gint score)
185 {
186     TomoeCandidatePrivate *priv;
187 
188     g_return_if_fail (TOMOE_IS_CANDIDATE (cand));
189 
190     priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
191     g_return_if_fail (priv);
192 
193     priv->score = score;
194 
195     g_object_notify (G_OBJECT (cand), "score");
196 }
197 
198 gint
tomoe_candidate_get_score(TomoeCandidate * cand)199 tomoe_candidate_get_score (TomoeCandidate *cand)
200 {
201     TomoeCandidatePrivate *priv;
202 
203     g_return_val_if_fail (TOMOE_IS_CANDIDATE (cand), 0);
204 
205     priv = TOMOE_CANDIDATE_GET_PRIVATE (cand);
206     g_return_val_if_fail (priv, 0);
207 
208     return priv->score;
209 }
210 
211 /**
212  * tomoe_candidate_compare:
213  * @a: 1st TomoeCandidate object to compare.
214  * @b: 2nd TomoeCandidate object to compare.
215  *
216  * Compare two TomoeCandidate.
217  *
218  * Return value: -1 a < b, 0 a= b, 1 a > b
219  */
220 gint
tomoe_candidate_compare(const TomoeCandidate * a,const TomoeCandidate * b)221 tomoe_candidate_compare (const TomoeCandidate *a, const TomoeCandidate *b)
222 {
223     TomoeCandidatePrivate *priv_a, *priv_b;
224 
225     if (!TOMOE_IS_CANDIDATE (a) || !TOMOE_IS_CANDIDATE (b)) return 0;
226 
227     priv_a = TOMOE_CANDIDATE_GET_PRIVATE (a);
228     priv_b = TOMOE_CANDIDATE_GET_PRIVATE (b);
229     if (!priv_a || !priv_b) return 0;
230 
231     return priv_a->score > priv_b->score ? 1
232          : priv_a->score < priv_b->score ? - 1
233          : 0;
234 }
235 
236 /*
237 vi:ts=4:nowrap:ai:expandtab
238 */
239