* added scripts/modest-parse-presets.pl, src/modest-presets.[ch]:
authorDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Fri, 3 Nov 2006 15:12:14 +0000 (15:12 +0000)
committerDirk-Jan C. Binnema <dirk-jan.binnema@nokia.com>
Fri, 3 Nov 2006 15:12:14 +0000 (15:12 +0000)
  imported first iteration of the presets system, which will allow modest
  to use preset provider info (mailservers etc.). the system uses an existing
  excel-sheet with this information, and the perl script translates this into
  a GKeyFile understood by modest-presets.[ch]. This excelsheet has not been
  checked-in yet.

pmo-trunk-r479

scripts/modest-parse-presets.pl [new file with mode: 0755]
src/modest-presets.c [new file with mode: 0644]
src/modest-presets.h [new file with mode: 0644]

diff --git a/scripts/modest-parse-presets.pl b/scripts/modest-parse-presets.pl
new file mode 100755 (executable)
index 0000000..59df154
--- /dev/null
@@ -0,0 +1,161 @@
+#! /usr/bin/perl -w
+
+# Copyright (c) 2006, Nokia Corporation
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+# * Redistributions of source code must retain the above copyright
+#   notice, this list of conditions and the following disclaimer.
+# * Redistributions in binary form must reproduce the above copyright
+#   notice, this list of conditions and the following disclaimer in the
+#   documentation and/or other materials provided with the distribution.
+# * Neither the name of the Nokia Corporation nor the names of its
+#   contributors may be used to endorse or promote products derived from
+#   this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+#
+
+#
+# Perl script to converts an Excel spreadsheet (arg 1)
+# with provider information in a GKeyFile readable format
+# for use in modest (see modest-presets.[ch] for details]
+# The input is assumed to be MS-style UTF16-LE, and
+# output will be UTF-8. 
+##
+# Program requires Spreadsheet::ParseExcel, which does all
+# the interesting stuff
+
+# input columns are like this:
+# 0: MCC (0 For Global Email)
+# 1: Mailbox Name
+# 2: EmailAddress
+# 3: OutgoingMailServer
+# 4: Secure smtp: (0 = no\, 1=yes)
+# 5: IncomingMailServer
+# 6: SendMessage (0=Immediately; 1=During next conn.)
+# 7: SendCopyToSelf (0=No; 1=Yes)
+# 8: MailboxType (0=POP3; 1=IMAP4)
+# 9: Security (0=Off; 1=On(143/110); 2=On(993/995);)
+#10: APOPSecureLogin (0=Off; 1=On)
+#11: RetrieveAttachment (0=No; 1=Yes)
+#12: RetrieveHeaders (0=All; 1-99=User defined)
+
+# some of these are properties of user-settings; however,
+# we are only interested in server settings, so some
+# of the data (6,7,11,12) will be ignored for our output.
+
+use strict;
+use Spreadsheet::ParseExcel;
+#use Unicode::String qw(utf8 utf16le);
+#Unicode::String->stringify_as('utf8');
+
+die "usage: xls2cvs <file.xls>\n" unless @ARGV == 1;
+
+my $file = $ARGV[0];
+die "'$file' is not a readable file\n" unless -r $file;
+  
+
+my $xl   = new Spreadsheet::ParseExcel;
+my $data = $xl->Parse($file) or die "could not parse $file: $!";
+
+my $sheet = $data->{Worksheet}[0];
+
+my $now = `date`;
+chomp $now;
+
+print "# generated on $now from " . $data->{File} . "\n";
+print "# keys and their meaning:\n";
+print "# [MailboxName]: name of the provider (eg. Wanadoo, Gmail)\n" .
+      "# MCC: Mobile Country Code (Netherlands=204, France=208, ...\n" . 
+      "#                           globals like GMail don't have one)\n" .
+      "# OutgoingMailServer: name of the smtp server (eg. smtp.foo.fi)\n" . 
+      "# SecureSMTP: 'true' if there's secure SMTP\n" .
+      "# IncomingMailServer\n" .
+      "# MailboxType: 'pop' or 'imap'\n" .
+      "# SMTPSecurity: 'true' if SMTP is secure\n" .
+      "# APOPSecureLogin: 'true' if APOP is supported\n\n";
+
+# ignore the first row
+for (my $r = $sheet->{MinRow} + 1 ; defined $sheet->{MaxRow} && $r <= $sheet->{MaxRow}; ++$r) {
+
+    my $cell;
+
+    # legend:
+    # 0: MCC (mobile country code, or 0 for Global)
+    # 1: MailboxName
+    # 2: EmailAddress
+    # 3: OutgoingMailServer
+    # 4: SecureSmtp
+    # 5: IncomingMailServer
+    # 6: SendMessage
+    # 7:
+    
+    next unless ($sheet->{Cells}[$r][0] && $sheet->{Cells}[$r][0]->Value =~ /\d+/);
+
+    # name -> required, unique
+    $cell = $sheet->{Cells}[$r][1];
+    next unless ($cell);
+    print "[" . $cell->Value . "]\n";
+
+    # MCC -> TODO: convert to normal country code
+    $cell = $sheet->{Cells}[$r][0];
+    if ($cell->Value > 0) {
+       print "MCC=" . $cell->Value . "\n";
+    }
+
+    # address -> required, unique
+    #$cell = $sheet->{Cells}[$r][2];
+    #print "EmailAddress=" . $cell->Value . "\n";
+
+    # OutgoingMailServer
+    $cell = $sheet->{Cells}[$r][3];
+    print "OutgoingMailServer=" . $cell->Value . "\n" if ($cell);
+
+    # SecureSmtp?
+    $cell = $sheet->{Cells}[$r][4];
+    print "SecureSmtp=true\n"  if ($cell && $cell->Value == 1);
+    
+    # IncomingMailServer
+    $cell = $sheet->{Cells}[$r][5];
+    if ($cell) {
+       print "IncomingMailServer=" . $cell->Value;
+       
+       my $type = $sheet->{Cells}[$r][8]->Value;
+       my $sec =  $sheet->{Cells}[$r][9]->Value;
+       
+       if ($sec == 2) {
+           if ($type == 0) { print ":995";}
+           if ($type == 1) { print ":993";}
+       }
+       print "\n";
+       print "IncomingSecurity=$sec\n";
+    }
+    
+    # MailboxType
+    $cell = $sheet->{Cells}[$r][8];
+    if ($cell) {
+       print "MailboxType=";
+       if ($cell->Value == '0') {print "pop"} else {print "imap"};
+       print "\n";
+    }
+
+    $cell = $sheet->{Cells}[$r][10];
+    print "APOPSecureLogin=true\n"  if ($cell && $cell->Value == 1);
+}
+
+# the end
diff --git a/src/modest-presets.c b/src/modest-presets.c
new file mode 100644 (file)
index 0000000..081a9b4
--- /dev/null
@@ -0,0 +1,179 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <string.h> /* for strcmp */
+#include "modest-presets.h"
+
+#define MODEST_PRESETS_KEY_MCC               "MCC"
+#define MODEST_PRESETS_KEY_INCOMING          "IncomingMailServer"
+#define MODEST_PRESETS_KEY_OUTGOING          "OutgoingMailServer"
+#define MODEST_PRESETS_KEY_MAILBOX_TYPE      "MailboxType"
+#define MODEST_PRESETS_KEY_MAILBOX_TYPE_POP  "pop"
+#define MODEST_PRESETS_KEY_MAILBOX_TYPE_IMAP "imap"
+#define MODEST_PRESETS_KEY_APOP              "APOPSecureLogin"
+#define MODEST_PRESETS_KEY_SECURE_SMTP       "SecureSMTP"
+#define MODEST_PRESETS_KEY_TRUE                     "true"
+
+ModestPresets*
+modest_presets_new (const gchar *presetfile)
+{
+       ModestPresets *presets = NULL;
+       GError        *err     = NULL;
+       
+       g_return_val_if_fail (presetfile, NULL);
+       
+       presets = g_new (ModestPresets, 1);
+       presets->keyfile = g_key_file_new ();
+
+       if (!presets->keyfile) {
+               g_printerr ("modest: cannot instantiate GKeyFile\n");
+               g_free (presets);
+               return NULL;
+       }
+
+       if (!g_key_file_load_from_file (presets->keyfile, presetfile,
+                                       G_KEY_FILE_NONE, &err)) {
+               g_printerr ("modest: cannot open keyfile: %s\n",
+                           err ? err->message : "unknown reason");
+               g_error_free (err);
+               g_free (presets);
+               return NULL;
+       }
+
+       return presets;
+}
+
+gchar**
+modest_presets_get_providers  (ModestPresets *self, gint mcc, gboolean include_globals)
+{
+       gchar **providers = NULL;
+       gchar **filtered  = NULL;
+       
+       g_return_val_if_fail (self && self->keyfile, NULL);
+
+       providers = g_key_file_get_groups (self->keyfile, NULL);
+
+       /* return *all* providers? */
+       if (mcc < 0)
+               return providers;
+
+       /* nope: filter them instead */
+       filtered = g_new(gchar*, g_strv_length(providers));
+
+       if (filtered && providers) {
+               int i = 0, j = 0;
+               while (providers[i]) {
+
+                       int this_mcc;
+                       this_mcc = g_key_file_get_integer (self->keyfile, providers[i],
+                                                          MODEST_PRESETS_KEY_MCC,
+                                                          NULL);
+                       if (this_mcc == mcc || (this_mcc == 0 && include_globals)) {
+                               filtered[j++] = providers[i];
+                               providers[i] = NULL; /*  g_strfreev: leave it alone */
+                       }
+                       ++i;
+               }
+       }
+       g_strfreev (providers);
+       
+       return filtered;
+}
+
+
+gchar*
+modest_presets_get_server (ModestPresets *self, const gchar *provider,
+                          gboolean incoming_server)
+{      
+       g_return_val_if_fail (self && self->keyfile, NULL);
+       g_return_val_if_fail (provider, NULL);
+
+       return g_key_file_get_string (self->keyfile, provider, 
+                                     incoming_server ?
+                                     MODEST_PRESETS_KEY_INCOMING :
+                                     MODEST_PRESETS_KEY_OUTGOING,
+                                     NULL);
+}
+
+
+ModestPresetsInfo
+modest_presets_get_info (ModestPresets *self, const gchar *provider, gboolean incoming_server)
+{
+       ModestPresetsInfo info = 0;
+       gchar *val = NULL;
+       
+       g_return_val_if_fail (self && self->keyfile, 0);
+
+       val = g_key_file_get_string (self->keyfile, provider,
+                                       MODEST_PRESETS_KEY_INCOMING, NULL);
+       if (val) {
+               g_free (val);
+               val = g_key_file_get_string (self->keyfile, provider,
+                                            MODEST_PRESETS_KEY_MAILBOX_TYPE, NULL);
+               if (strcmp (val, MODEST_PRESETS_KEY_MAILBOX_TYPE_POP) == 0)
+                       info |= MODEST_PRESETS_INFO_POP;
+               if (strcmp (val, MODEST_PRESETS_KEY_MAILBOX_TYPE_IMAP) == 0)
+                       info |= MODEST_PRESETS_INFO_IMAP;
+               g_free (val);
+
+               val = g_key_file_get_string (self->keyfile, provider,
+                                            MODEST_PRESETS_KEY_APOP, NULL);
+               if (val && strcmp(val, MODEST_PRESETS_KEY_TRUE) == 0)
+                       info |= MODEST_PRESETS_INFO_APOP;
+               g_free(val);
+       }
+               
+
+       val = g_key_file_get_string (self->keyfile, provider,
+                                    MODEST_PRESETS_KEY_OUTGOING, NULL);
+       if (val) {
+               g_free (val);
+               info |= MODEST_PRESETS_INFO_SMTP;
+               
+               val = g_key_file_get_string (self->keyfile, provider,
+                                            MODEST_PRESETS_KEY_SECURE_SMTP, NULL);
+               if (val && strcmp(val,MODEST_PRESETS_KEY_TRUE) == 0)
+                       info |= MODEST_PRESETS_INFO_SECURE_SMTP;
+               g_free(val);
+       }
+
+       return info;
+}
+       
+       
+void
+modest_presets_destroy (ModestPresets *self)
+{
+       g_return_if_fail (self);
+
+       g_key_file_free (self->keyfile);
+       self->keyfile = NULL;
+       
+       g_free (self);
+}
diff --git a/src/modest-presets.h b/src/modest-presets.h
new file mode 100644 (file)
index 0000000..b542fdf
--- /dev/null
@@ -0,0 +1,125 @@
+/* Copyright (c) 2006, Nokia Corporation
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above copyright
+ *   notice, this list of conditions and the following disclaimer in the
+ *   documentation and/or other materials provided with the distribution.
+ * * Neither the name of the Nokia Corporation nor the names of its
+ *   contributors may be used to endorse or promote products derived from
+ *   this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
+ * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
+ * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
+ * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __MODEST_PRESETS_H__
+#define __MODEST_PRESETS_H__
+
+#include <glib.h>
+
+struct _ModestPresets {
+/* private data: don't touch */
+       GKeyFile *keyfile;
+};
+typedef struct _ModestPresets ModestPresets;
+
+enum _ModestPresetsInfo {
+       /* two bits for the server type */
+       MODEST_PRESETS_INFO_NONE             = 0x0000,
+       MODEST_PRESETS_INFO_IMAP             = 0x0001,
+       MODEST_PRESETS_INFO_POP              = 0x0002,
+       MODEST_PRESETS_INFO_SMTP             = 0x0003,
+
+       /* one bit for each of these */
+       MODEST_PRESETS_INFO_APOP             = 0x0004,
+       MODEST_PRESETS_INFO_SECURE_SMTP      = 0x0008,
+       MODEST_PRESETS_INFO_SECURE_INCOMING  = 0x000f   
+};
+
+typedef enum _ModestPresetsInfo ModestPresetsInfo;
+
+
+/**
+ * modest_presets_new:
+ * @presetfile: the full path to the file with presets (in GKeyFile format)
+ * 
+ * make a new ModestPresets instance
+ *
+ * Returns: a new ModestPresets instance, or NULL in case of error.
+ */
+ModestPresets*            modest_presets_new             (const gchar *presetfile);
+
+
+/**
+ * modest_presets_get_providers:
+ * @self: a valid ModestPresets instance
+ * @mcc: limit the search to providers with this mcc (Mobile Country Code),
+ *       or <0 to get all
+ * @include_globals: include providers without MCC (such as GMail, Yahoo) if @mcc != 0?
+ * 
+ * get a list of providers
+ *
+ * Returns: a newly allocated array of strings, or NULL in case of error
+ * should be freed with g_strvfree
+ * 
+ **/
+gchar **                  modest_presets_get_providers   (ModestPresets *self, gint mcc,
+                                                         gboolean include_globals);
+
+/**
+ * modest_presets_get_server:
+ * @self: a valid ModestPresets instance
+ * @provider: name of the provider 
+ * @incoming: get the incoming mailserver if TRUE, get the outgoing server otherwise
+ *
+ * get the name of a incoming or outgoing mailserver
+ * 
+ * Returns: a newly allocated string with the servername, or NULL in case
+ * of error, or server not found. (FIXME). Note that if the (incoming) server uses a
+ * non-standard port, the port number is appended to the name, eg. pop.foo.fi:995
+ */
+gchar *                   modest_presets_get_server      (ModestPresets *self,
+                                                         const gchar *provider,
+                                                         gboolean incoming_server);
+/**
+ * modest_presets_get_info:
+ * @self: a valid ModestPresets instance
+ * @provider: name of the provider 
+ * @incoming: get the incoming mailserver if TRUE, get the outgoing server otherwise
+ *
+ * get information about some incoming or outgoing mailserver
+ *
+ * Returns: a ModestPresetsInfo with the required information
+ */
+ModestPresetsInfo          modest_presets_get_info (ModestPresets *self,
+                                                   const gchar *provider,
+                                                   gboolean incoming_server);
+
+/**
+ * modest_presets_destroy:
+ * @self: a valid ModestPresets instance
+ *
+ * destroy ModestPresets instance; this is required after you're done with it.
+ *
+ */
+void                      modest_presets_destroy         (ModestPresets *self);
+
+
+#endif /*__MODEST_PRESETS__*/
+
+