GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
business-urls.c
1 /*
2  * business-urls.c -- Initialize HTML for business code
3  *
4  * Written By: Derek Atkins <[email protected]>
5  * Copyright (C) 2002 Derek Atkins
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License as
9  * published by the Free Software Foundation; either version 2 of
10  * the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, contact:
19  *
20  * Free Software Foundation Voice: +1-617-542-5942
21  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
22  * Boston, MA 02110-1301, USA [email protected]
23  */
24 
25 #include "config.h"
26 
27 #include <gtk/gtk.h>
28 #include <glib/gi18n.h>
29 
30 #include "gnc-html.h"
31 #include "gnc-ui-util.h"
32 #include "qof.h"
33 
34 #include "gncCustomer.h"
35 #include "gncJob.h"
36 #include "gncVendor.h"
37 #include "gncEmployee.h"
38 #include "gncInvoice.h"
39 
40 #include "business-urls.h"
41 #include "dialog-customer.h"
42 #include "dialog-employee.h"
43 #include "dialog-vendor.h"
44 #include "dialog-invoice.h"
45 #include "dialog-job.h"
46 
47 /* Disable -Waddress. GCC 4.2 warns (and fails to compile with -Werror) when
48  * passing the address of a guid on the stack to QOF_BOOK_LOOKUP_ENTITY via
49  * gncInvoiceLookup and friends. When the macro gets inlined, the compiler
50  * emits a warning that the guid null pointer test is always true.
51  */
52 #if (__GNUC__ >= 4 && __GNUC_MINOR__ >= 2)
53 # pragma GCC diagnostic warning "-Waddress"
54 #endif
55 
56 #define HANDLE_TYPE(URL_TYPE_STR,OBJ_TYPE) { \
57  QofBook *book; \
58  GncGUID guid; \
59  QofCollection *coll; \
60  \
61  g_return_val_if_fail (location != NULL, FALSE); \
62  g_return_val_if_fail (result != NULL, FALSE); \
63  result->load_to_stream = FALSE; \
64  \
65  if (strncmp (URL_TYPE_STR, location, strlen(URL_TYPE_STR))) \
66  { \
67  result->error_message = \
68  g_strdup_printf (_("Badly formed URL %s"), location); \
69  return FALSE; \
70  } \
71  if (!string_to_guid (location + strlen(URL_TYPE_STR), &guid)) \
72  { \
73  result->error_message = g_strdup_printf (_("Bad URL: %s"), location); \
74  return FALSE; \
75  } \
76  \
77  book = gnc_get_current_book(); \
78  coll = qof_book_get_collection (book, OBJ_TYPE); \
79  entity = qof_collection_lookup_entity (coll, &guid); \
80  if (NULL == entity) \
81  { \
82  result->error_message = g_strdup_printf (_("No such entity: %s"), \
83  location); \
84  return FALSE; \
85  } \
86 }
87 
88 static gboolean
89 customerCB (const char *location, const char *label,
90  gboolean new_window, GNCURLResult * result)
91 {
92  QofInstance *entity;
93  GncCustomer *customer;
94 
95  /* href="...:customer=<guid>" */
96  HANDLE_TYPE ("customer=", GNC_ID_CUSTOMER);
97  customer = (GncCustomer *) entity;
98  gnc_ui_customer_edit (customer);
99 
100  return TRUE;
101 }
102 
103 static gboolean
104 vendorCB (const char *location, const char *label,
105  gboolean new_window, GNCURLResult * result)
106 {
107  QofInstance *entity;
108  GncVendor *vendor;
109 
110  /* href="...:vendor=<guid>" */
111  HANDLE_TYPE ("vendor=", GNC_ID_VENDOR);
112  vendor = (GncVendor *) entity;
113  gnc_ui_vendor_edit (vendor);
114 
115  return TRUE;
116 }
117 
118 static gboolean
119 employeeCB (const char *location, const char *label,
120  gboolean new_window, GNCURLResult * result)
121 {
122  QofInstance *entity;
123  GncEmployee *employee;
124 
125  /* href="...:employee=<guid>" */
126  HANDLE_TYPE ("employee=", GNC_ID_EMPLOYEE);
127 
128  employee = (GncEmployee *) entity;
129  gnc_ui_employee_edit (employee);
130 
131  return TRUE;
132 }
133 
134 static gboolean
135 invoiceCB (const char *location, const char *label,
136  gboolean new_window, GNCURLResult * result)
137 {
138  QofInstance *entity;
139  GncInvoice *invoice;
140 
141  /* href="...:invoice=<guid>" */
142  HANDLE_TYPE ("invoice=", GNC_ID_INVOICE);
143  invoice = (GncInvoice *) entity;
144  gnc_ui_invoice_edit (invoice);
145 
146  return TRUE;
147 }
148 
149 static gboolean
150 jobCB (const char *location, const char *label,
151  gboolean new_window, GNCURLResult * result)
152 {
153  QofInstance *entity;
154  GncJob *job;
155 
156  /* href="...:job=<guid>" */
157  HANDLE_TYPE ("job=", GNC_ID_JOB);
158  job = (GncJob *) entity;
159  gnc_ui_job_edit (job);
160 
161  return TRUE;
162 }
163 
164 /* ================================================================= */
165 
166 #define RETURN_IF_NULL(inst) \
167  if (NULL == inst) \
168  { \
169  result->error_message = \
170  g_strdup_printf (_("No such owner entity: %s"), location); \
171  return FALSE; \
172  }
173 
174 static gboolean
175 ownerreportCB (const char *location, const char *label,
176  gboolean new_window, GNCURLResult * result)
177 {
178  const char *ownerptr;
179  const char *acctptr;
180  GncGUID guid;
181  GncOwner owner;
182  GncOwnerType type;
183  char *etype = NULL;
184  Account *acc = NULL;
185 
186  g_return_val_if_fail (location != NULL, FALSE);
187  g_return_val_if_fail (result != NULL, FALSE);
188 
189  result->load_to_stream = FALSE;
190 
191  /* href="...:owner=<owner-type>:guid=<guid>[&acct=<guid>]" */
192 
193  acctptr = strchr (location, '&');
194  if (acctptr)
195  acctptr++;
196 
197  if (strncmp ("owner=", location, 6) != 0)
198  {
199  result->error_message = g_strdup_printf (_("Badly formed URL %s"),
200  location);
201  return FALSE;
202  }
203 
204  memset (&owner, 0, sizeof (owner));
205 
206  ownerptr = location + 6;
207  switch (*ownerptr)
208  {
209  case 'c':
210  type = GNC_OWNER_CUSTOMER;
211  break;
212  case 'v':
213  type = GNC_OWNER_VENDOR;
214  break;
215  case 'e':
216  type = GNC_OWNER_EMPLOYEE;
217  break;
218  case 'j':
219  type = GNC_OWNER_JOB;
220  break;
221  default:
222  result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
223  return FALSE;
224  }
225 
226  if (!string_to_guid (ownerptr + 2, &guid))
227  {
228  result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
229  return FALSE;
230  }
231 
232 
233  switch (type)
234  {
235  case GNC_OWNER_CUSTOMER:
236  {
237  GncCustomer *customer =
238  gncCustomerLookup (gnc_get_current_book (), &guid);
239  RETURN_IF_NULL (customer);
240  gncOwnerInitCustomer (&owner, customer);
241  etype = "Customer";
242  break;
243  }
244  case GNC_OWNER_VENDOR:
245  {
246  GncVendor *vendor =
247  gncVendorLookup (gnc_get_current_book (), &guid);
248  RETURN_IF_NULL (vendor);
249  gncOwnerInitVendor (&owner, vendor);
250  etype = "Vendor";
251  break;
252  }
253  case GNC_OWNER_EMPLOYEE:
254  {
255  GncEmployee *employee =
256  gncEmployeeLookup (gnc_get_current_book (), &guid);
257  RETURN_IF_NULL(employee);
258  gncOwnerInitEmployee (&owner, employee);
259  etype = "Employee";
260  break;
261  }
262  case GNC_OWNER_JOB:
263  {
264  GncJob *job =
265  gncJobLookup (gnc_get_current_book (), &guid);
266  RETURN_IF_NULL(job);
267  gncOwnerInitJob (&owner, job);
268  etype = "Job";
269  break;
270  }
271  default:
272  etype = "OTHER";
273  break;
274  }
275 
276  if (owner.owner.undefined == NULL)
277  {
278  result->error_message =
279  g_strdup_printf (_("Entity type does not match %s: %s"),
280  etype, location);
281  return FALSE;
282  }
283 
284  /* Deal with acctptr, if it exists */
285  if (acctptr)
286  {
287  if (strncmp ("acct=", acctptr, 5) != 0)
288  {
289  result->error_message = g_strdup_printf (_("Bad URL %s"), location);
290  return FALSE;
291  }
292 
293  if (!string_to_guid (acctptr + 5, &guid))
294  {
295  result->error_message = g_strdup_printf (_("Bad URL: %s"), location);
296  return FALSE;
297  }
298 
299  acc = xaccAccountLookup (&guid, gnc_get_current_book ());
300  if (NULL == acc)
301  {
302  result->error_message = g_strdup_printf (_("No such Account entity: %s"),
303  location);
304  return FALSE;
305  }
306  }
307 
308  /* Ok, let's run this report */
309  gnc_business_call_owner_report (&owner, acc);
310 
311  return TRUE;
312 }
313 
314 void
315 gnc_business_urls_initialize (void)
316 {
317  int i;
318  static struct
319  {
320  URLType urltype;
321  char * protocol;
322  GncHTMLUrlCB handler;
323  } types[] =
324  {
325  { GNC_ID_CUSTOMER, GNC_ID_CUSTOMER, customerCB },
326  { GNC_ID_VENDOR, GNC_ID_VENDOR, vendorCB },
327  { GNC_ID_EMPLOYEE, GNC_ID_EMPLOYEE, employeeCB },
328  { GNC_ID_JOB, GNC_ID_JOB, jobCB },
329  { GNC_ID_INVOICE, GNC_ID_INVOICE, invoiceCB },
330  { URL_TYPE_OWNERREPORT, "gnc-ownerreport", ownerreportCB },
331  { NULL, NULL }
332  };
333 
334  for (i = 0; types[i].urltype; i++)
335  gnc_html_register_urltype (types[i].urltype, types[i].protocol);
336 
337  for (i = 0; types[i].urltype; i++)
338  if (types[i].handler)
339  gnc_html_register_url_handler (types[i].urltype, types[i].handler);
340 
341 }
342 
343 /* =========================== END OF FILE ========================= */
Core Customer Interface.
utility functions for the GnuCash UI
gboolean string_to_guid(const gchar *string, GncGUID *guid)
Definition: guid.h:65
Definition: gncJob.c:41
Business Invoice Interface.
Job Interface.
Employee Interface.
Vendor Interface.
Account * xaccAccountLookup(const GncGUID *guid, QofBook *book)
Definition: Account.c:1827