1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2008 <jrliggett@cox.net>
5  *
6  * anjuta is free software.
7  *
8  * You may redistribute it and/or modify it under the terms of the
9  * GNU General Public License, as published by the Free Software
10  * Foundation; either version 2 of the License, or (at your option)
11  * any later version.
12  *
13  * anjuta is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with anjuta.  If not, write to:
20  * 	The Free Software Foundation, Inc.,
21  * 	51 Franklin Street, Fifth Floor
22  * 	Boston, MA  02110-1301, USA.
23  */
24 
25 #include "git-revision.h"
26 
27 struct _GitRevisionPriv
28 {
29 	gchar *sha;
30 	gchar *author;
31 	gchar *date;
32 	gchar *short_log;
33 	GList *children;
34 	gboolean has_parents;
35 };
36 
37 G_DEFINE_TYPE (GitRevision, git_revision, G_TYPE_OBJECT);
38 
39 static void
git_revision_init(GitRevision * self)40 git_revision_init (GitRevision *self)
41 {
42 	self->priv = g_new0 (GitRevisionPriv, 1);
43 }
44 
45 static void
git_revision_finalize(GObject * object)46 git_revision_finalize (GObject *object)
47 {
48 	GitRevision *self;
49 
50 	self = GIT_REVISION (object);
51 
52 	g_free (self->priv->sha);
53 	g_free (self->priv->author);
54 	g_free (self->priv->date);
55 	g_free (self->priv->short_log);
56 
57 	g_list_free (self->priv->children);
58 	g_free (self->priv);
59 
60 	G_OBJECT_CLASS (git_revision_parent_class)->finalize (object);
61 }
62 
63 static void
git_revision_class_init(GitRevisionClass * klass)64 git_revision_class_init (GitRevisionClass *klass)
65 {
66 	GObjectClass* object_class = G_OBJECT_CLASS (klass);
67 
68 	object_class->finalize = git_revision_finalize;
69 }
70 
71 GitRevision *
git_revision_new(void)72 git_revision_new (void)
73 {
74 	return g_object_new (GIT_TYPE_REVISION, NULL);
75 }
76 
77 void
git_revision_set_sha(GitRevision * self,const gchar * sha)78 git_revision_set_sha (GitRevision *self, const gchar *sha)
79 {
80 	g_free (self->priv->sha);
81 	self->priv->sha = g_strdup (sha);
82 }
83 
84 gchar *
git_revision_get_sha(GitRevision * self)85 git_revision_get_sha (GitRevision *self)
86 {
87 	return g_strdup (self->priv->sha);
88 }
89 
90 gchar *
git_revision_get_short_sha(GitRevision * self)91 git_revision_get_short_sha (GitRevision *self)
92 {
93 	return g_strndup (self->priv->sha, 7);
94 }
95 
96 void
git_revision_set_author(GitRevision * self,const gchar * author)97 git_revision_set_author (GitRevision *self, const gchar *author)
98 {
99 	g_free (self->priv->author);
100 	self->priv->author = g_strdup (author);
101 }
102 
103 gchar *
git_revision_get_author(GitRevision * self)104 git_revision_get_author (GitRevision *self)
105 {
106 	return g_strdup (self->priv->author);
107 }
108 
109 void
git_revision_set_short_log(GitRevision * self,const gchar * short_log)110 git_revision_set_short_log (GitRevision *self, const gchar *short_log)
111 {
112 	g_free (self->priv->short_log);
113 	self->priv->short_log = g_strdup (short_log);
114 
115 	g_strchug (self->priv->short_log);
116 }
117 
118 gchar *
git_revision_get_short_log(GitRevision * self)119 git_revision_get_short_log (GitRevision *self)
120 {
121 	return g_strdup (self->priv->short_log);
122 }
123 
124 static const gchar *
git_revision_get_time_format(struct tm * revision_time)125 git_revision_get_time_format (struct tm *revision_time)
126 {
127 	struct tm *tm;
128 	time_t t1, t2;
129 
130 	t1 = mktime ((struct tm *) revision_time);
131 
132 	/* check whether it's ahead in time */
133 	time (&t2);
134 	if (t1 > t2)
135 	{
136 		return "%c";
137 	}
138 
139 	/* check whether it's as fresh as today's bread */
140 	t2 = time (NULL);
141 	tm = localtime (&t2);
142 	tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
143 	t2 = mktime (tm);
144 
145 	if (t1 > t2)
146 	{
147 		/* TRANSLATORS: it's a strftime format string */
148 		return "%I:%M %p";
149 	}
150 
151 	/* check whether it's older than a week */
152 	t2 = time (NULL);
153 	tm = localtime (&t2);
154 	tm->tm_sec = tm->tm_min = tm->tm_hour = 0;
155 	t2 = mktime (tm);
156 
157 	t2 -= 60 * 60 * 24 * 6; /* substract 1 week */
158 
159 	if (t1 > t2)
160 	{
161 		/* TRANSLATORS: it's a strftime format string */
162 		return "%a %I:%M %p";
163 	}
164 
165 	/* check whether it's more recent than the new year hangover */
166 	t2 = time (NULL);
167 	tm = localtime (&t2);
168 	tm->tm_sec = tm->tm_min = tm->tm_hour = tm->tm_mon = 0;
169 	tm->tm_mday = 1;
170 	t2 = mktime (tm);
171 
172 	if (t1 > t2)
173 	{
174 		/* TRANSLATORS: it's a strftime format string */
175 		return "%b %d %I:%M %p";
176 	}
177 
178 	/* it's older */
179 	/* TRANSLATORS: it's a strftime format string */
180 	return "%b %d %Y";
181 }
182 
183 void
git_revision_set_date(GitRevision * self,time_t unix_time)184 git_revision_set_date (GitRevision *self, time_t unix_time)
185 {
186 	struct tm time;
187 	const gchar *format;
188 	gchar buffer[256];
189 
190 	localtime_r (&unix_time, &time);
191 	format = git_revision_get_time_format (&time);
192 	strftime (buffer, sizeof (buffer), format, &time);
193 
194 	g_free (self->priv->date);
195 	self->priv->date = g_strdup (buffer);
196 }
197 
198 gchar *
git_revision_get_formatted_date(GitRevision * self)199 git_revision_get_formatted_date (GitRevision *self)
200 {
201 	return g_strdup (self->priv->date);
202 }
203 
204 void
git_revision_add_child(GitRevision * self,GitRevision * child)205 git_revision_add_child (GitRevision *self, GitRevision *child)
206 {
207 	self->priv->children = g_list_prepend (self->priv->children,
208 										  child);
209 	git_revision_set_has_parents (child, TRUE);
210 }
211 
212 GList *
git_revision_get_children(GitRevision * self)213 git_revision_get_children (GitRevision *self)
214 {
215 	return self->priv->children;
216 }
217 
218 void
git_revision_set_has_parents(GitRevision * self,gboolean has_parents)219 git_revision_set_has_parents (GitRevision *self, gboolean has_parents)
220 {
221 	self->priv->has_parents = has_parents;
222 }
223 
224 gboolean
git_revision_has_parents(GitRevision * self)225 git_revision_has_parents (GitRevision *self)
226 {
227 	return self->priv->has_parents;
228 }
229