00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 #include "dialog-job.h"
00046
00047
00048
00049
00050
00051
00052 #if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 2)
00053 # pragma GCC diagnostic warning "-Waddress"
00054 #endif
00055
00056 #define HANDLE_TYPE(URL_TYPE_STR,OBJ_TYPE) { \
00057 QofBook *book; \
00058 GncGUID guid; \
00059 QofCollection *coll; \
00060 \
00061 g_return_val_if_fail (location != NULL, FALSE); \
00062 g_return_val_if_fail (result != NULL, FALSE); \
00063 result->load_to_stream = FALSE; \
00064 \
00065 if (strncmp (URL_TYPE_STR, location, strlen(URL_TYPE_STR))) \
00066 { \
00067 result->error_message = \
00068 g_strdup_printf (_("Badly formed URL %s"), location); \
00069 return FALSE; \
00070 } \
00071 if (!string_to_guid (location + strlen(URL_TYPE_STR), &guid)) \
00072 { \
00073 result->error_message = g_strdup_printf (_("Bad URL: %s"), location); \
00074 return FALSE; \
00075 } \
00076 \
00077 book = gnc_get_current_book(); \
00078 coll = qof_book_get_collection (book, OBJ_TYPE); \
00079 entity = qof_collection_lookup_entity (coll, &guid); \
00080 if (NULL == entity) \
00081 { \
00082 result->error_message = g_strdup_printf (_("No such entity: %s"), \
00083 location); \
00084 return FALSE; \
00085 } \
00086 }
00087
00088 static gboolean
00089 customerCB (const char *location, const char *label,
00090 gboolean new_window, GNCURLResult * result)
00091 {
00092 QofInstance *entity;
00093 GncCustomer *customer;
00094
00095
00096 HANDLE_TYPE ("customer=", GNC_ID_CUSTOMER);
00097 customer = (GncCustomer *) entity;
00098 gnc_ui_customer_edit (customer);
00099
00100 return TRUE;
00101 }
00102
00103 static gboolean
00104 vendorCB (const char *location, const char *label,
00105 gboolean new_window, GNCURLResult * result)
00106 {
00107 QofInstance *entity;
00108 GncVendor *vendor;
00109
00110
00111 HANDLE_TYPE ("vendor=", GNC_ID_VENDOR);
00112 vendor = (GncVendor *) entity;
00113 gnc_ui_vendor_edit (vendor);
00114
00115 return TRUE;
00116 }
00117
00118 static gboolean
00119 employeeCB (const char *location, const char *label,
00120 gboolean new_window, GNCURLResult * result)
00121 {
00122 QofInstance *entity;
00123 GncEmployee *employee;
00124
00125
00126 HANDLE_TYPE ("employee=", GNC_ID_EMPLOYEE);
00127
00128 employee = (GncEmployee *) entity;
00129 gnc_ui_employee_edit (employee);
00130
00131 return TRUE;
00132 }
00133
00134 static gboolean
00135 invoiceCB (const char *location, const char *label,
00136 gboolean new_window, GNCURLResult * result)
00137 {
00138 QofInstance *entity;
00139 GncInvoice *invoice;
00140
00141
00142 HANDLE_TYPE ("invoice=", GNC_ID_INVOICE);
00143 invoice = (GncInvoice *) entity;
00144 gnc_ui_invoice_edit (invoice);
00145
00146 return TRUE;
00147 }
00148
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
00157 HANDLE_TYPE ("job=", GNC_ID_JOB);
00158 job = (GncJob *) entity;
00159 gnc_ui_job_edit (job);
00160
00161 return TRUE;
00162 }
00163
00164
00165
00166 #define RETURN_IF_NULL(inst) \
00167 if (NULL == inst) \
00168 { \
00169 result->error_message = \
00170 g_strdup_printf (_("No such owner entity: %s"), location); \
00171 return FALSE; \
00172 }
00173
00174 static gboolean
00175 ownerreportCB (const char *location, const char *label,
00176 gboolean new_window, GNCURLResult * result)
00177 {
00178 const char *ownerptr;
00179 const char *acctptr;
00180 GncGUID guid;
00181 GncOwner owner;
00182 GncOwnerType type;
00183 char *etype = NULL;
00184 Account *acc = NULL;
00185
00186 g_return_val_if_fail (location != NULL, FALSE);
00187 g_return_val_if_fail (result != NULL, FALSE);
00188
00189 result->load_to_stream = FALSE;
00190
00191
00192
00193 acctptr = strchr (location, '&');
00194 if (acctptr)
00195 acctptr++;
00196
00197 if (strncmp ("owner=", location, 6) != 0)
00198 {
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 {
00209 case 'c':
00210 type = GNC_OWNER_CUSTOMER;
00211 break;
00212 case 'v':
00213 type = GNC_OWNER_VENDOR;
00214 break;
00215 case 'e':
00216 type = GNC_OWNER_EMPLOYEE;
00217 break;
00218 case 'j':
00219 type = GNC_OWNER_JOB;
00220 break;
00221 default:
00222 result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00223 return FALSE;
00224 }
00225
00226 if (!string_to_guid (ownerptr + 2, &guid))
00227 {
00228 result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00229 return FALSE;
00230 }
00231
00232
00233 switch (type)
00234 {
00235 case GNC_OWNER_CUSTOMER:
00236 {
00237 GncCustomer *customer =
00238 gncCustomerLookup (gnc_get_current_book (), &guid);
00239 RETURN_IF_NULL (customer);
00240 gncOwnerInitCustomer (&owner, customer);
00241 etype = "Customer";
00242 break;
00243 }
00244 case GNC_OWNER_VENDOR:
00245 {
00246 GncVendor *vendor =
00247 gncVendorLookup (gnc_get_current_book (), &guid);
00248 RETURN_IF_NULL (vendor);
00249 gncOwnerInitVendor (&owner, vendor);
00250 etype = "Vendor";
00251 break;
00252 }
00253 case GNC_OWNER_EMPLOYEE:
00254 {
00255 GncEmployee *employee =
00256 gncEmployeeLookup (gnc_get_current_book (), &guid);
00257 RETURN_IF_NULL(employee);
00258 gncOwnerInitEmployee (&owner, employee);
00259 etype = "Employee";
00260 break;
00261 }
00262 case GNC_OWNER_JOB:
00263 {
00264 GncJob *job =
00265 gncJobLookup (gnc_get_current_book (), &guid);
00266 RETURN_IF_NULL(job);
00267 gncOwnerInitJob (&owner, job);
00268 etype = "Job";
00269 break;
00270 }
00271 default:
00272 etype = "OTHER";
00273 }
00274
00275 if (owner.owner.undefined == NULL)
00276 {
00277 result->error_message =
00278 g_strdup_printf (_("Entity type does not match %s: %s"),
00279 etype, location);
00280 return FALSE;
00281 }
00282
00283
00284 if (acctptr)
00285 {
00286 if (strncmp ("acct=", acctptr, 5) != 0)
00287 {
00288 result->error_message = g_strdup_printf (_("Bad URL %s"), location);
00289 return FALSE;
00290 }
00291
00292 if (!string_to_guid (acctptr + 5, &guid))
00293 {
00294 result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
00295 return FALSE;
00296 }
00297
00298 acc = xaccAccountLookup (&guid, gnc_get_current_book ());
00299 if (NULL == acc)
00300 {
00301 result->error_message = g_strdup_printf (_("No such Account entity: %s"),
00302 location);
00303 return FALSE;
00304 }
00305 }
00306
00307
00308 gnc_business_call_owner_report (&owner, acc);
00309
00310 return TRUE;
00311 }
00312
00313 void
00314 gnc_business_urls_initialize (void)
00315 {
00316 int i;
00317 static struct
00318 {
00319 URLType urltype;
00320 char * protocol;
00321 GncHTMLUrlCB handler;
00322 } types[] =
00323 {
00324 { GNC_ID_CUSTOMER, GNC_ID_CUSTOMER, customerCB },
00325 { GNC_ID_VENDOR, GNC_ID_VENDOR, vendorCB },
00326 { GNC_ID_EMPLOYEE, GNC_ID_EMPLOYEE, employeeCB },
00327 { GNC_ID_JOB, GNC_ID_JOB, jobCB },
00328 { GNC_ID_INVOICE, GNC_ID_INVOICE, invoiceCB },
00329 { URL_TYPE_OWNERREPORT, "gnc-ownerreport", ownerreportCB },
00330 { NULL, NULL }
00331 };
00332
00333 for (i = 0; types[i].urltype; i++)
00334 gnc_html_register_urltype (types[i].urltype, types[i].protocol);
00335
00336 for (i = 0; types[i].urltype; i++)
00337 if (types[i].handler)
00338 gnc_html_register_url_handler (types[i].urltype, types[i].handler);
00339
00340 }
00341
00342