|
GnuCash 2.4.99
|
00001 /********************************************************************\ 00002 * gncJob.c -- the Core Job Interface * 00003 * * 00004 * This program is free software; you can redistribute it and/or * 00005 * modify it under the terms of the GNU General Public License as * 00006 * published by the Free Software Foundation; either version 2 of * 00007 * the License, or (at your option) any later version. * 00008 * * 00009 * This program is distributed in the hope that it will be useful, * 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00012 * GNU General Public License for more details. * 00013 * * 00014 * You should have received a copy of the GNU General Public License* 00015 * along with this program; if not, contact: * 00016 * * 00017 * Free Software Foundation Voice: +1-617-542-5942 * 00018 * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 * 00019 * Boston, MA 02110-1301, USA gnu@gnu.org * 00020 * * 00021 \********************************************************************/ 00022 00023 /* 00024 * Copyright (C) 2001, 2002 Derek Atkins 00025 * Copyright (C) 2003 Linas Vepstas <linas@linas.org> 00026 * Author: Derek Atkins <warlord@MIT.EDU> 00027 */ 00028 00029 #include "config.h" 00030 00031 #include <glib.h> 00032 #include <string.h> 00033 00034 #include "gncInvoice.h" 00035 #include "gncJob.h" 00036 #include "gncJobP.h" 00037 #include "gncOwnerP.h" 00038 00039 struct _gncJob 00040 { 00041 QofInstance inst; 00042 char * id; 00043 char * name; 00044 char * desc; 00045 GncOwner owner; 00046 gboolean active; 00047 }; 00048 00049 struct _gncJobClass 00050 { 00051 QofInstanceClass parent_class; 00052 }; 00053 00054 static QofLogModule log_module = GNC_MOD_BUSINESS; 00055 00056 #define _GNC_MOD_NAME GNC_ID_JOB 00057 00058 /* ================================================================== */ 00059 /* misc inline functions */ 00060 00061 G_INLINE_FUNC void mark_job (GncJob *job); 00062 void mark_job (GncJob *job) 00063 { 00064 qof_instance_set_dirty(&job->inst); 00065 qof_event_gen (&job->inst, QOF_EVENT_MODIFY, NULL); 00066 } 00067 00068 /* ================================================================== */ 00069 00070 enum 00071 { 00072 PROP_0, 00073 PROP_NAME 00074 }; 00075 00076 /* GObject Initialization */ 00077 G_DEFINE_TYPE(GncJob, gnc_job, QOF_TYPE_INSTANCE); 00078 00079 static void 00080 gnc_job_init(GncJob* job) 00081 { 00082 } 00083 00084 static void 00085 gnc_job_dispose(GObject *jobp) 00086 { 00087 G_OBJECT_CLASS(gnc_job_parent_class)->dispose(jobp); 00088 } 00089 00090 static void 00091 gnc_job_finalize(GObject* jobp) 00092 { 00093 G_OBJECT_CLASS(gnc_job_parent_class)->finalize(jobp); 00094 } 00095 00096 static void 00097 gnc_job_get_property (GObject *object, 00098 guint prop_id, 00099 GValue *value, 00100 GParamSpec *pspec) 00101 { 00102 GncJob *job; 00103 00104 g_return_if_fail(GNC_IS_JOB(object)); 00105 00106 job = GNC_JOB(object); 00107 switch (prop_id) 00108 { 00109 case PROP_NAME: 00110 g_value_set_string(value, job->name); 00111 break; 00112 default: 00113 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); 00114 break; 00115 } 00116 } 00117 00118 static void 00119 gnc_job_set_property (GObject *object, 00120 guint prop_id, 00121 const GValue *value, 00122 GParamSpec *pspec) 00123 { 00124 GncJob *job; 00125 00126 g_return_if_fail(GNC_IS_JOB(object)); 00127 00128 job = GNC_JOB(object); 00129 switch (prop_id) 00130 { 00131 case PROP_NAME: 00132 gncJobSetName(job, g_value_get_string(value)); 00133 break; 00134 default: 00135 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); 00136 break; 00137 } 00138 } 00139 00146 static GList* 00147 impl_get_typed_referring_object_list(const QofInstance* inst, const QofInstance* ref) 00148 { 00149 /* Refers to nothing */ 00150 return NULL; 00151 } 00152 00153 static void 00154 gnc_job_class_init (GncJobClass *klass) 00155 { 00156 GObjectClass *gobject_class = G_OBJECT_CLASS (klass); 00157 QofInstanceClass* qof_class = QOF_INSTANCE_CLASS(klass); 00158 00159 gobject_class->dispose = gnc_job_dispose; 00160 gobject_class->finalize = gnc_job_finalize; 00161 gobject_class->set_property = gnc_job_set_property; 00162 gobject_class->get_property = gnc_job_get_property; 00163 00164 qof_class->get_display_name = NULL; 00165 qof_class->refers_to_object = NULL; 00166 qof_class->get_typed_referring_object_list = impl_get_typed_referring_object_list; 00167 00168 g_object_class_install_property 00169 (gobject_class, 00170 PROP_NAME, 00171 g_param_spec_string ("name", 00172 "Job Name", 00173 "The job name is an arbitrary string " 00174 "assigned by the user. It is intended to " 00175 "a short character string that is displayed " 00176 "by the GUI as the job mnemonic.", 00177 NULL, 00178 G_PARAM_READWRITE)); 00179 } 00180 00181 /* Create/Destroy Functions */ 00182 GncJob *gncJobCreate (QofBook *book) 00183 { 00184 GncJob *job; 00185 00186 if (!book) return NULL; 00187 00188 job = g_object_new (GNC_TYPE_JOB, NULL); 00189 qof_instance_init_data (&job->inst, _GNC_MOD_NAME, book); 00190 00191 job->id = CACHE_INSERT (""); 00192 job->name = CACHE_INSERT (""); 00193 job->desc = CACHE_INSERT (""); 00194 job->active = TRUE; 00195 00196 /* GncOwner not initialized */ 00197 qof_event_gen (&job->inst, QOF_EVENT_CREATE, NULL); 00198 00199 return job; 00200 } 00201 00202 void gncJobDestroy (GncJob *job) 00203 { 00204 if (!job) return; 00205 qof_instance_set_destroying(job, TRUE); 00206 gncJobCommitEdit (job); 00207 } 00208 00209 static void gncJobFree (GncJob *job) 00210 { 00211 if (!job) return; 00212 00213 qof_event_gen (&job->inst, QOF_EVENT_DESTROY, NULL); 00214 00215 CACHE_REMOVE (job->id); 00216 CACHE_REMOVE (job->name); 00217 CACHE_REMOVE (job->desc); 00218 00219 switch (gncOwnerGetType (&(job->owner))) 00220 { 00221 case GNC_OWNER_CUSTOMER: 00222 gncCustomerRemoveJob (gncOwnerGetCustomer(&job->owner), job); 00223 break; 00224 case GNC_OWNER_VENDOR: 00225 gncVendorRemoveJob (gncOwnerGetVendor(&job->owner), job); 00226 break; 00227 default: 00228 break; 00229 } 00230 00231 /* qof_instance_release (&job->inst); */ 00232 g_object_unref (job); 00233 } 00234 00235 00236 /* ================================================================== */ 00237 /* Set Functions */ 00238 00239 #define SET_STR(obj, member, str) { \ 00240 char * tmp; \ 00241 \ 00242 if (!safe_strcmp (member, str)) return; \ 00243 gncJobBeginEdit (obj); \ 00244 tmp = CACHE_INSERT (str); \ 00245 CACHE_REMOVE (member); \ 00246 member = tmp; \ 00247 } 00248 00249 void gncJobSetID (GncJob *job, const char *id) 00250 { 00251 if (!job) return; 00252 if (!id) return; 00253 SET_STR(job, job->id, id); 00254 mark_job (job); 00255 gncJobCommitEdit (job); 00256 } 00257 00258 void gncJobSetName (GncJob *job, const char *name) 00259 { 00260 if (!job) return; 00261 if (!name) return; 00262 SET_STR(job, job->name, name); 00263 mark_job (job); 00264 gncJobCommitEdit (job); 00265 } 00266 00267 void gncJobSetReference (GncJob *job, const char *desc) 00268 { 00269 if (!job) return; 00270 if (!desc) return; 00271 SET_STR(job, job->desc, desc); 00272 mark_job (job); 00273 gncJobCommitEdit (job); 00274 } 00275 00276 void gncJobSetOwner (GncJob *job, GncOwner *owner) 00277 { 00278 if (!job) return; 00279 if (!owner) return; 00280 if (gncOwnerEqual (owner, &(job->owner))) return; 00281 00282 switch (gncOwnerGetType (owner)) 00283 { 00284 case GNC_OWNER_CUSTOMER: 00285 case GNC_OWNER_VENDOR: 00286 break; 00287 default: 00288 PERR("Unsupported Owner type: %d", gncOwnerGetType(owner)); 00289 return; 00290 } 00291 00292 gncJobBeginEdit (job); 00293 00294 switch (gncOwnerGetType (&(job->owner))) 00295 { 00296 case GNC_OWNER_CUSTOMER: 00297 gncCustomerRemoveJob (gncOwnerGetCustomer(&job->owner), job); 00298 break; 00299 case GNC_OWNER_VENDOR: 00300 gncVendorRemoveJob (gncOwnerGetVendor(&job->owner), job); 00301 break; 00302 default: 00303 break; 00304 } 00305 00306 gncOwnerCopy (owner, &(job->owner)); 00307 00308 switch (gncOwnerGetType (&(job->owner))) 00309 { 00310 case GNC_OWNER_CUSTOMER: 00311 gncCustomerAddJob (gncOwnerGetCustomer(&job->owner), job); 00312 break; 00313 case GNC_OWNER_VENDOR: 00314 gncVendorAddJob (gncOwnerGetVendor(&job->owner), job); 00315 break; 00316 default: 00317 break; 00318 } 00319 00320 mark_job (job); 00321 gncJobCommitEdit (job); 00322 } 00323 00324 void gncJobSetActive (GncJob *job, gboolean active) 00325 { 00326 if (!job) return; 00327 if (active == job->active) return; 00328 gncJobBeginEdit (job); 00329 job->active = active; 00330 mark_job (job); 00331 gncJobCommitEdit (job); 00332 } 00333 00334 static void 00335 qofJobSetOwner (GncJob *job, QofInstance *ent) 00336 { 00337 if (!job || !ent) 00338 { 00339 return; 00340 } 00341 qof_begin_edit(&job->inst); 00342 qofOwnerSetEntity(&job->owner, ent); 00343 mark_job (job); 00344 qof_commit_edit(&job->inst); 00345 } 00346 00347 void gncJobBeginEdit (GncJob *job) 00348 { 00349 qof_begin_edit(&job->inst); 00350 } 00351 00352 static void gncJobOnError (QofInstance *inst, QofBackendError errcode) 00353 { 00354 PERR("Job QofBackend Failure: %d", errcode); 00355 gnc_engine_signal_commit_error( errcode ); 00356 } 00357 00358 static void job_free (QofInstance *inst) 00359 { 00360 GncJob *job = (GncJob *)inst; 00361 gncJobFree (job); 00362 } 00363 00364 static void gncJobOnDone (QofInstance *qof) { } 00365 00366 void gncJobCommitEdit (GncJob *job) 00367 { 00368 if (!qof_commit_edit (QOF_INSTANCE(job))) return; 00369 qof_commit_edit_part2 (&job->inst, gncJobOnError, 00370 gncJobOnDone, job_free); 00371 } 00372 00373 /* ================================================================== */ 00374 /* Get Functions */ 00375 00376 const char * gncJobGetID (const GncJob *job) 00377 { 00378 if (!job) return NULL; 00379 return job->id; 00380 } 00381 00382 const char * gncJobGetName (const GncJob *job) 00383 { 00384 if (!job) return NULL; 00385 return job->name; 00386 } 00387 00388 const char * gncJobGetReference (const GncJob *job) 00389 { 00390 if (!job) return NULL; 00391 return job->desc; 00392 } 00393 00394 GncOwner * gncJobGetOwner (GncJob *job) 00395 { 00396 if (!job) return NULL; 00397 return &(job->owner); 00398 } 00399 00400 gboolean gncJobGetActive (const GncJob *job) 00401 { 00402 if (!job) return FALSE; 00403 return job->active; 00404 } 00405 00406 static QofInstance* 00407 qofJobGetOwner (GncJob *job) 00408 { 00409 if (!job) 00410 { 00411 return NULL; 00412 } 00413 return QOF_INSTANCE(qofOwnerGetOwner(&job->owner)); 00414 } 00415 00416 /* Other functions */ 00417 00418 int gncJobCompare (const GncJob * a, const GncJob *b) 00419 { 00420 if (!a && !b) return 0; 00421 if (!a && b) return 1; 00422 if (a && !b) return -1; 00423 00424 return (safe_strcmp(a->id, b->id)); 00425 } 00426 00427 gboolean gncJobEqual(const GncJob * a, const GncJob *b) 00428 { 00429 if (a == NULL && b == NULL) return TRUE; 00430 if (a == NULL || b == NULL) return FALSE; 00431 00432 g_return_val_if_fail(GNC_IS_JOB(a), FALSE); 00433 g_return_val_if_fail(GNC_IS_JOB(b), FALSE); 00434 00435 if (safe_strcmp(a->id, b->id) != 0) 00436 { 00437 PWARN("IDs differ: %s vs %s", a->id, b->id); 00438 return FALSE; 00439 } 00440 00441 if (safe_strcmp(a->name, b->name) != 0) 00442 { 00443 PWARN("Names differ: %s vs %s", a->name, b->name); 00444 return FALSE; 00445 } 00446 00447 if (safe_strcmp(a->desc, b->desc) != 0) 00448 { 00449 PWARN("Descriptions differ: %s vs %s", a->desc, b->desc); 00450 return FALSE; 00451 } 00452 00453 if (a->active != b->active) 00454 { 00455 PWARN("Active flags differ"); 00456 return FALSE; 00457 } 00458 00459 /* FIXME: Need real tests */ 00460 #if 0 00461 GncOwner owner; 00462 #endif 00463 00464 return TRUE; 00465 } 00466 00467 /* ================================================================== */ 00468 /* Package-Private functions */ 00469 00470 static const char * _gncJobPrintable (gpointer item) 00471 { 00472 GncJob *c; 00473 if (!item) return NULL; 00474 c = item; 00475 return c->name; 00476 } 00477 00478 static void 00479 destroy_job_on_book_close(QofInstance *ent, gpointer data) 00480 { 00481 GncJob* job = GNC_JOB(ent); 00482 00483 gncJobFree(job); 00484 } 00485 00486 static QofObject gncJobDesc = 00487 { 00488 DI(.interface_version = ) QOF_OBJECT_VERSION, 00489 DI(.e_type = ) _GNC_MOD_NAME, 00490 DI(.type_label = ) "Job", 00491 DI(.create = ) (gpointer)gncJobCreate, 00492 DI(.book_begin = ) NULL, 00493 DI(.book_end = ) NULL, 00494 DI(.is_dirty = ) qof_collection_is_dirty, 00495 DI(.mark_clean = ) qof_collection_mark_clean, 00496 DI(.foreach = ) qof_collection_foreach, 00497 DI(.printable = ) _gncJobPrintable, 00498 DI(.version_cmp = ) (int (*)(gpointer, gpointer)) qof_instance_version_cmp, 00499 }; 00500 00501 gboolean gncJobRegister (void) 00502 { 00503 static QofParam params[] = 00504 { 00505 { JOB_ID, QOF_TYPE_STRING, (QofAccessFunc)gncJobGetID, (QofSetterFunc)gncJobSetID }, 00506 { JOB_NAME, QOF_TYPE_STRING, (QofAccessFunc)gncJobGetName, (QofSetterFunc)gncJobSetName }, 00507 { JOB_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncJobGetActive, (QofSetterFunc)gncJobSetActive }, 00508 { JOB_REFERENCE, QOF_TYPE_STRING, (QofAccessFunc)gncJobGetReference, (QofSetterFunc)gncJobSetReference }, 00509 #ifdef GNUCASH_MAJOR_VERSION 00510 { JOB_OWNER, GNC_ID_OWNER, (QofAccessFunc)gncJobGetOwner, NULL }, 00511 #else 00512 { JOB_OWNER, QOF_TYPE_CHOICE, (QofAccessFunc)qofJobGetOwner, (QofSetterFunc)qofJobSetOwner }, 00513 #endif 00514 { QOF_PARAM_ACTIVE, QOF_TYPE_BOOLEAN, (QofAccessFunc)gncJobGetActive, NULL }, 00515 { QOF_PARAM_BOOK, QOF_ID_BOOK, (QofAccessFunc)qof_instance_get_book, NULL }, 00516 { QOF_PARAM_GUID, QOF_TYPE_GUID, (QofAccessFunc)qof_instance_get_guid, NULL }, 00517 { NULL }, 00518 }; 00519 00520 if (!qof_choice_create(GNC_ID_JOB)) 00521 { 00522 return FALSE; 00523 } 00524 if (!qof_choice_add_class(GNC_ID_INVOICE, GNC_ID_JOB, INVOICE_OWNER)) 00525 { 00526 return FALSE; 00527 } 00528 00529 qof_class_register (_GNC_MOD_NAME, (QofSortFunc)gncJobCompare, params); 00530 #ifdef GNUCASH_MAJOR_VERSION 00531 qofJobGetOwner(NULL); 00532 qofJobSetOwner(NULL, NULL); 00533 #endif 00534 return qof_object_register (&gncJobDesc); 00535 } 00536 00537 gchar *gncJobNextID (QofBook *book) 00538 { 00539 return qof_book_increment_and_format_counter (book, _GNC_MOD_NAME); 00540 }
1.7.4