1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * anjuta
4  * Copyright (C) James Liggett 2007 <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 "svn-log-entry.h"
26 
27 struct _SvnLogEntryPriv
28 {
29 	gchar *author;
30 	gchar *date;
31 	glong revision;
32 	gchar *log;
33 	gchar *short_log;
34 };
35 
36 G_DEFINE_TYPE (SvnLogEntry, svn_log_entry, G_TYPE_OBJECT);
37 
38 static void
svn_log_entry_init(SvnLogEntry * self)39 svn_log_entry_init (SvnLogEntry *self)
40 {
41 	self->priv = g_new0 (SvnLogEntryPriv, 1);
42 }
43 
44 static void
svn_log_entry_finalize(GObject * object)45 svn_log_entry_finalize (GObject *object)
46 {
47 	SvnLogEntry *self;
48 
49 	self = SVN_LOG_ENTRY (object);
50 
51 	g_free (self->priv->author);
52 	g_free (self->priv->date);
53 	g_free (self->priv->log);
54 	g_free (self->priv->short_log);
55 	g_free (self->priv);
56 
57 	G_OBJECT_CLASS (svn_log_entry_parent_class)->finalize (object);
58 }
59 
60 static void
svn_log_entry_class_init(SvnLogEntryClass * klass)61 svn_log_entry_class_init (SvnLogEntryClass *klass)
62 {
63 	GObjectClass *object_class = G_OBJECT_CLASS (klass);
64 
65 	object_class->finalize = svn_log_entry_finalize;
66 }
67 
68 static gchar *
strip_whitespace(gchar * buffer)69 strip_whitespace (gchar *buffer)
70 {
71 	gchar *buffer_pos;
72 
73 	buffer_pos = buffer;
74 
75 	while (buffer_pos)
76 	{
77 		if (g_ascii_isspace (*buffer_pos))
78 			buffer_pos++;
79 		else
80 			break;
81 	}
82 
83 	return buffer_pos;
84 }
85 
86 SvnLogEntry *
svn_log_entry_new(gchar * author,gchar * date,glong revision,gchar * log)87 svn_log_entry_new (gchar *author, gchar *date, glong revision, gchar *log)
88 {
89 	SvnLogEntry *self;
90 	gchar *log_filtered;  /* No leading whitespace */
91 	gchar *first_newline;
92 	size_t first_newline_pos;
93 	gchar *first_log_line;
94 	gchar *short_log;
95 
96 	self = g_object_new (SVN_TYPE_LOG_ENTRY, NULL);
97 	self->priv->author = g_strdup (author);
98 	self->priv->date = g_strdup (date);
99 	self->priv->revision = revision;
100 	self->priv->log = g_strdup (log);
101 
102 	/* Now create the "short log", or a one-line summary of a log.
103 	 * This is just the first line of a commit log message. If there is more
104 	 * than one line to a message, take the first line and put an ellipsis
105 	 * after it to create the short log. Otherwise, the short log is just a
106 	 * copy of the log messge. */
107 	log_filtered = strip_whitespace (log);
108 	first_newline = strchr (log_filtered, '\n');
109 
110 	if (first_newline)
111 	{
112 		first_newline_pos = first_newline - log_filtered;
113 
114 		if (first_newline_pos < (strlen (log_filtered) - 1))
115 		{
116 			first_log_line = g_strndup (log_filtered, first_newline_pos);
117 			short_log = g_strconcat (first_log_line, " ...", NULL);
118 			g_free (first_log_line);
119 		}
120 		else /* There could be a newline and nothing after it... */
121 			short_log = g_strndup (log_filtered, first_newline_pos);
122 	}
123 	else
124 		short_log = g_strdup (log_filtered);
125 
126 	self->priv->short_log = g_strdup (short_log);
127 	g_free (short_log);
128 
129 	return self;
130 }
131 
132 void
svn_log_entry_destroy(SvnLogEntry * self)133 svn_log_entry_destroy (SvnLogEntry *self)
134 {
135 	g_object_unref (self);
136 }
137 
138 gchar *
svn_log_entry_get_author(SvnLogEntry * self)139 svn_log_entry_get_author (SvnLogEntry *self)
140 {
141 	return g_strdup (self->priv->author);
142 }
143 
144 gchar *
svn_log_entry_get_date(SvnLogEntry * self)145 svn_log_entry_get_date (SvnLogEntry *self)
146 {
147 	return g_strdup (self->priv->date);
148 }
149 
150 glong
svn_log_entry_get_revision(SvnLogEntry * self)151 svn_log_entry_get_revision (SvnLogEntry *self)
152 {
153 	return self->priv->revision;
154 }
155 
156 gchar *
svn_log_entry_get_short_log(SvnLogEntry * self)157 svn_log_entry_get_short_log (SvnLogEntry *self)
158 {
159 	return g_strdup (self->priv->short_log);
160 }
161 
162 gchar *
svn_log_entry_get_full_log(SvnLogEntry * self)163 svn_log_entry_get_full_log (SvnLogEntry *self)
164 {
165 	return g_strdup (self->priv->log);
166 }
167