summaryrefslogtreecommitdiff
path: root/src/camel/camel-nntp-address.c
diff options
context:
space:
mode:
authorMilan Crha <mcrha@redhat.com>2016-10-11 11:47:14 +0200
committerMilan Crha <mcrha@redhat.com>2016-10-11 11:47:14 +0200
commitd7931c6dd9db1e090f4bb466983c3dced19e2201 (patch)
tree31e31eef195355e800c63be6b4dcfefe6e37bb84 /src/camel/camel-nntp-address.c
parent4febe3ae82e850ca9f17229dd2dbd9cdd8708a8f (diff)
downloadevolution-data-server-d7931c6dd9db1e090f4bb466983c3dced19e2201.tar.gz
Reorganize directory structure
Let's have it as it's common to be, which means top level src/ for sources, single data/ for data, and so on.
Diffstat (limited to 'src/camel/camel-nntp-address.c')
-rw-r--r--src/camel/camel-nntp-address.c187
1 files changed, 187 insertions, 0 deletions
diff --git a/src/camel/camel-nntp-address.c b/src/camel/camel-nntp-address.c
new file mode 100644
index 000000000..3e16a29cb
--- /dev/null
+++ b/src/camel/camel-nntp-address.c
@@ -0,0 +1,187 @@
+/*
+ * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
+ *
+ * This library 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.
+ *
+ * This library 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 Lesser General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library. If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Michael Zucchi <notzed@ximian.com>
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "camel-mime-utils.h"
+#include "camel-nntp-address.h"
+
+#define d(x)
+
+struct _address {
+ gchar *name;
+ gchar *address;
+};
+
+G_DEFINE_TYPE (CamelNNTPAddress, camel_nntp_address, CAMEL_TYPE_ADDRESS)
+
+/* since newsgropus are 7bit ascii, decode/unformat are the same */
+static gint
+nntp_address_decode (CamelAddress *address,
+ const gchar *raw)
+{
+ GSList *ha, *n;
+ gint count = address->addresses->len;
+
+ ha = camel_header_newsgroups_decode (raw);
+ for (n = ha; n != NULL; n = n->next) {
+ camel_nntp_address_add (CAMEL_NNTP_ADDRESS (address), n->data);
+ }
+
+ g_slist_free_full (ha, g_free);
+ return address->addresses->len - count;
+}
+
+/* since newsgropus are 7bit ascii, encode/format are the same */
+static gchar *
+nntp_address_encode (CamelAddress *address)
+{
+ gint i;
+ GString *out;
+ gchar *ret;
+
+ if (address->addresses->len == 0)
+ return NULL;
+
+ out = g_string_new ("");
+
+ for (i = 0; i < address->addresses->len; i++) {
+ if (i != 0)
+ g_string_append (out, ", ");
+
+ g_string_append (out, g_ptr_array_index (address->addresses, i));
+ }
+
+ ret = out->str;
+ g_string_free (out, FALSE);
+
+ return ret;
+}
+
+static gint
+nntp_address_cat (CamelAddress *dest,
+ CamelAddress *source)
+{
+ gint ii;
+
+ g_return_val_if_fail (CAMEL_IS_NNTP_ADDRESS (source), -1);
+
+ for (ii = 0; ii < source->addresses->len; ii++)
+ camel_nntp_address_add (
+ CAMEL_NNTP_ADDRESS (dest),
+ g_ptr_array_index (source->addresses, ii));
+
+ return ii;
+}
+
+static void
+nntp_address_remove (CamelAddress *address,
+ gint index)
+{
+ if (index < 0 || index >= address->addresses->len)
+ return;
+
+ g_free (g_ptr_array_index (address->addresses, index));
+ g_ptr_array_remove_index (address->addresses, index);
+}
+
+static void
+camel_nntp_address_class_init (CamelNNTPAddressClass *class)
+{
+ CamelAddressClass *address_class;
+
+ address_class = CAMEL_ADDRESS_CLASS (class);
+ address_class->decode = nntp_address_decode;
+ address_class->encode = nntp_address_encode;
+ address_class->unformat = nntp_address_decode;
+ address_class->format = nntp_address_encode;
+ address_class->remove = nntp_address_remove;
+ address_class->cat = nntp_address_cat;
+}
+
+static void
+camel_nntp_address_init (CamelNNTPAddress *nntp_address)
+{
+}
+
+/**
+ * camel_nntp_address_new:
+ *
+ * Create a new CamelNNTPAddress object.
+ *
+ * Returns: A new CamelNNTPAddress object.
+ **/
+CamelNNTPAddress *
+camel_nntp_address_new (void)
+{
+ return g_object_new (CAMEL_TYPE_NNTP_ADDRESS, NULL);
+}
+
+/**
+ * camel_nntp_address_add:
+ * @a: nntp address object
+ * @name:
+ *
+ * Add a new nntp address to the address object. Duplicates are not added twice.
+ *
+ * Returns: Index of added entry, or existing matching entry.
+ **/
+gint
+camel_nntp_address_add (CamelNNTPAddress *a,
+ const gchar *name)
+{
+ gint index, i;
+
+ g_return_val_if_fail (CAMEL_IS_NNTP_ADDRESS (a), -1);
+
+ index = ((CamelAddress *) a)->addresses->len;
+ for (i = 0; i < index; i++)
+ if (!strcmp (g_ptr_array_index (((CamelAddress *) a)->addresses, i), name))
+ return i;
+
+ g_ptr_array_add (((CamelAddress *) a)->addresses, g_strdup (name));
+
+ return index;
+}
+
+/**
+ * camel_nntp_address_get:
+ * @a: nntp address object
+ * @index: address's array index
+ * @namep: Holder for the returned address, or NULL, if not required.
+ *
+ * Get the address at @index.
+ *
+ * Returns: TRUE if such an address exists, or FALSE otherwise.
+ **/
+gboolean
+camel_nntp_address_get (CamelNNTPAddress *a,
+ gint index,
+ const gchar **namep)
+{
+ g_return_val_if_fail (CAMEL_IS_NNTP_ADDRESS (a), FALSE);
+
+ if (index < 0 || index >= ((CamelAddress *) a)->addresses->len)
+ return FALSE;
+
+ if (namep)
+ *namep = g_ptr_array_index( ((CamelAddress *)a)->addresses, index);
+
+ return TRUE;
+}