business-urls.c

00001 /*
00002  * business-urls.c -- Initialize HTML for business code
00003  *
00004  * Written By: Derek Atkins <warlord@MIT.EDU>
00005  * Copyright (C) 2002 Derek Atkins
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License as
00009  * published by the Free Software Foundation; either version 2 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, contact:
00019  *
00020  * Free Software Foundation           Voice:  +1-617-542-5942
00021  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652
00022  * Boston, MA  02110-1301,  USA       gnu@gnu.org
00023  */
00024 
00025 #include "config.h"
00026 
00027 #include <gtk/gtk.h>
00028 #include <glib/gi18n.h>
00029 
00030 #include "gnc-html.h"
00031 #include "gnc-ui-util.h"
00032 #include "qof.h"
00033 
00034 #include "gncCustomer.h"
00035 #include "gncJob.h"
00036 #include "gncVendor.h"
00037 #include "gncEmployee.h"
00038 #include "gncInvoice.h"
00039 
00040 #include "business-urls.h"
00041 #include "dialog-customer.h"
00042 #include "dialog-employee.h"
00043 #include "dialog-vendor.h"
00044 #include "dialog-invoice.h"
00045 
00046 /* Disable -Waddress.  GCC 4.2 warns (and fails to compile with -Werror) when
00047  * passing the address of a guid on the stack to QOF_BOOK_LOOKUP_ENTITY via
00048  * gncInvoiceLookup and friends.  When the macro gets inlined, the compiler
00049  * emits a warning that the guid null pointer test is always true.
00050  */
00051 #if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 2)
00052 #    pragma GCC diagnostic ignored "-Waddress"
00053 #endif
00054 
00055 #define HANDLE_TYPE(URL_TYPE_STR,OBJ_TYPE) {                                 \
00056   QofBook *book;                                                             \
00057   GUID guid;                                                                 \
00058   QofCollection *coll;                                                       \
00059                                                                              \
00060   g_return_val_if_fail (location != NULL, FALSE);                            \
00061   g_return_val_if_fail (result != NULL, FALSE);                              \
00062   result->load_to_stream = FALSE;                                            \
00063                                                                              \
00064   if (strncmp (URL_TYPE_STR, location, strlen(URL_TYPE_STR)))                \
00065   {                                                                          \
00066     result->error_message =                                                  \
00067                     g_strdup_printf (_("Badly formed URL %s"), location);    \
00068     return FALSE;                                                            \
00069   }                                                                          \
00070   if (!string_to_guid (location + strlen(URL_TYPE_STR), &guid))              \
00071   {                                                                          \
00072     result->error_message = g_strdup_printf (_("Bad URL: %s"), location);    \
00073     return FALSE;                                                            \
00074   }                                                                          \
00075                                                                              \
00076   book = gnc_get_current_book();                                             \
00077   coll = qof_book_get_collection (book, OBJ_TYPE);                           \
00078   entity = qof_collection_lookup_entity (coll, &guid);                       \
00079   if (NULL == entity)                                                        \
00080   {                                                                          \
00081     result->error_message = g_strdup_printf (_("No such entity: %s"),        \
00082                 location);                                                   \
00083     return FALSE;                                                            \
00084   }                                                                          \
00085 }
00086 
00087 static gboolean
00088 customerCB (const char *location, const char *label,
00089            gboolean new_window, GNCURLResult * result)
00090 {
00091   QofInstance *entity;
00092   GncCustomer *customer;
00093 
00094   /* href="...:customer=<guid>" */
00095   HANDLE_TYPE ("customer=", GNC_ID_CUSTOMER);
00096   customer = (GncCustomer *) entity;
00097   gnc_ui_customer_edit (customer);
00098 
00099   return TRUE;
00100 }
00101 
00102 static gboolean
00103 vendorCB (const char *location, const char *label,
00104            gboolean new_window, GNCURLResult * result)
00105 {
00106   QofInstance *entity;
00107   GncVendor *vendor;
00108 
00109   /* href="...:vendor=<guid>" */
00110   HANDLE_TYPE ("vendor=", GNC_ID_VENDOR);
00111   vendor = (GncVendor *) entity;
00112   gnc_ui_vendor_edit (vendor);
00113 
00114   return TRUE;
00115 }
00116 
00117 static gboolean
00118 employeeCB (const char *location, const char *label,
00119            gboolean new_window, GNCURLResult * result)
00120 {
00121   QofInstance *entity;
00122   GncEmployee *employee;
00123 
00124   /* href="...:employee=<guid>" */
00125   HANDLE_TYPE ("employee=", GNC_ID_EMPLOYEE);
00126 
00127   employee = (GncEmployee *) entity;
00128   gnc_ui_employee_edit (employee);
00129 
00130   return TRUE;
00131 }
00132 
00133 static gboolean
00134 invoiceCB (const char *location, const char *label,
00135            gboolean new_window, GNCURLResult * result)
00136 {
00137   QofInstance *entity;
00138   GncInvoice *invoice;
00139 
00140   /* href="...:invoice=<guid>" */
00141   HANDLE_TYPE ("invoice=", GNC_ID_INVOICE);
00142   invoice = (GncInvoice *) entity;
00143   gnc_ui_invoice_edit (invoice);
00144 
00145   return TRUE;
00146 }
00147 
00148 #if 0   // whats up w/ that ?
00149 static gboolean
00150 jobCB (const char *location, const char *label,
00151            gboolean new_window, GNCURLResult * result)
00152 {
00153   QofInstance *entity;
00154   GncJob *job;
00155 
00156   /* href="...:job=<guid>" */
00157   HANDLE_TYPE ("job=", GNC_ID_INVOICE);
00158   job = (GncJob *) entity;
00159   gnc_ui_job_edit (job);
00160 
00161   return TRUE;
00162 }
00163 #endif
00164 
00165 /* ================================================================= */
00166 
00167 #define RETURN_IF_NULL(inst)                                            \
00168   if (NULL == inst)                                                     \
00169   {                                                                     \
00170     result->error_message =                                             \
00171       g_strdup_printf (_("No such owner entity: %s"), location);        \
00172     return FALSE;                                                       \
00173   }
00174 
00175 static gboolean
00176 ownerreportCB (const char *location, const char *label,
00177                gboolean new_window, GNCURLResult * result)
00178 {
00179   const char *ownerptr;
00180   const char *acctptr;
00181   GUID guid;
00182   GncOwner owner;
00183   GncOwnerType type;
00184   char *etype = NULL;
00185   Account *acc = NULL;
00186 
00187   g_return_val_if_fail (location != NULL, FALSE);
00188   g_return_val_if_fail (result != NULL, FALSE);
00189 
00190   result->load_to_stream = FALSE;
00191 
00192   /* href="...:owner=<owner-type>:guid=<guid>[&acct=<guid>]" */
00193   
00194   acctptr = strchr (location, '&');
00195   if (acctptr)
00196     acctptr++;
00197 
00198   if (strncmp ("owner=", location, 6) != 0) {
00199     result->error_message = g_strdup_printf (_("Badly formed URL %s"),
00200                                              location);
00201     return FALSE;
00202   }
00203 
00204   memset (&owner, 0, sizeof (owner));
00205     
00206   ownerptr = location+6;
00207   switch (*ownerptr) {
00208   case 'c':
00209     type = GNC_OWNER_CUSTOMER;
00210     break;
00211   case 'v':
00212     type = GNC_OWNER_VENDOR;
00213     break;
00214   case 'e':
00215     type = GNC_OWNER_EMPLOYEE;
00216     break;
00217   default:
00218     result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00219     return FALSE;
00220   }
00221 
00222   if (!string_to_guid (ownerptr+2, &guid)) {
00223     result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00224     return FALSE;
00225   }
00226 
00227 
00228   switch (type) {
00229     case GNC_OWNER_CUSTOMER:
00230     {
00231       GncCustomer *customer = 
00232                       gncCustomerLookup (gnc_get_current_book (), &guid);
00233       RETURN_IF_NULL (customer);
00234       gncOwnerInitCustomer (&owner, customer);
00235       etype = "Customer";
00236       break;
00237     }
00238     case GNC_OWNER_VENDOR:
00239     {
00240       GncVendor *vendor =
00241                             gncVendorLookup (gnc_get_current_book (), &guid);
00242       RETURN_IF_NULL (vendor);
00243         gncOwnerInitVendor (&owner, vendor);
00244       etype = "Vendor";
00245       break;
00246     }
00247     case GNC_OWNER_EMPLOYEE:
00248     {
00249       GncEmployee *employee = 
00250            gncEmployeeLookup (gnc_get_current_book (), &guid);
00251       RETURN_IF_NULL(employee);
00252       gncOwnerInitEmployee (&owner, employee);
00253       etype = "Employee";
00254       break;
00255     }
00256     default:
00257       etype = "OTHER";
00258   }
00259 
00260   if (owner.owner.undefined == NULL)
00261   {
00262     result->error_message =
00263       g_strdup_printf (_("Entity type does not match %s: %s"),
00264                        etype, location);
00265     return FALSE;
00266   }
00267 
00268   /* Deal with acctptr, if it exists */
00269   if (acctptr)
00270   {
00271     if (strncmp ("acct=", acctptr, 5) != 0)
00272     {
00273       result->error_message = g_strdup_printf (_("Bad URL %s"), location);
00274       return FALSE;
00275     }
00276 
00277     if (!string_to_guid (acctptr+5, &guid)) {
00278       result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00279       return FALSE;
00280     }
00281 
00282     acc = xaccAccountLookup (&guid, gnc_get_current_book ());
00283     if (NULL == acc)
00284     {
00285       result->error_message = g_strdup_printf (_("No such Account entity: %s"),
00286                                                location);
00287       return FALSE;
00288     }
00289   }
00290 
00291   /* Ok, let's run this report */
00292   gnc_business_call_owner_report (&owner, acc);
00293 
00294   return TRUE;
00295 }
00296 
00297 void
00298 gnc_business_urls_initialize (void)
00299 {
00300   int i;
00301   static struct {
00302     URLType urltype;
00303     char *  protocol;
00304     GncHTMLUrlCB handler;
00305   } types[] = {
00306     { GNC_ID_CUSTOMER, GNC_ID_CUSTOMER, customerCB },
00307     { GNC_ID_VENDOR, GNC_ID_VENDOR, vendorCB },
00308     { GNC_ID_EMPLOYEE, GNC_ID_EMPLOYEE, employeeCB },
00309     { GNC_ID_INVOICE, GNC_ID_INVOICE, invoiceCB },
00310     { URL_TYPE_OWNERREPORT, "gnc-ownerreport", ownerreportCB },
00311     { NULL, NULL }
00312   };
00313 
00314   for (i = 0; types[i].urltype; i++)
00315     gnc_html_register_urltype (types[i].urltype, types[i].protocol);
00316 
00317   for (i = 0; types[i].urltype; i++)
00318     if (types[i].handler)
00319       gnc_html_register_url_handler (types[i].urltype, types[i].handler);
00320 
00321 }
00322 
00323 /* =========================== END OF FILE ========================= */

Generated on Mon Sep 8 05:03:45 2008 for GnuCash by  doxygen 1.5.2