00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00024 #define _GNU_SOURCE
00025
00026 #include <fcntl.h>
00027 #include <string.h>
00028 #include <sys/ioctl.h>
00029
00030 #include <libermanifest/ermanifest.h>
00031
00032 #include "scbdoc.h"
00033 #include "scblog.h"
00034
00035
00036
00037 gboolean _doc_check_path(ScbPathPtr ptr)
00038 {
00039 if (NULL == ptr || strnlen(ptr->scbname, SCB_MAX_PATH) <= 0)
00040 {
00041 SCB_ERROR("Invalid path!");
00042 return FALSE;
00043 }
00044 return TRUE;
00045 }
00046
00047
00048
00049
00050 ScbDocPtr scb_doc_new()
00051 {
00052 ScbDocPtr ptr = g_new0(ScbDoc, 1);
00053
00054 if (NULL == ptr)
00055 {
00056 SCB_ERROR("Not enough memory!");
00057 return NULL;
00058 }
00059
00060 if (!scb_pages_new(&ptr->pages))
00061 {
00062 g_free(ptr);
00063 return NULL;
00064 }
00065
00066 scb_doc_init_context(ptr);
00067
00068 return ptr;
00069 }
00070
00071
00072 void scb_doc_free(ScbDocPtr ptr)
00073 {
00074 if (NULL == ptr)
00075 {
00076 return;
00077 }
00078
00079 scb_doc_free_context(ptr);
00080 scb_pages_free(&ptr->pages);
00081 g_free(ptr);
00082 }
00083
00084
00085
00086
00087
00088 ScbDocPtr scb_doc_open(ScbPathPtr ptr)
00089 {
00090
00091 if (!_doc_check_path(ptr)) return NULL;
00092
00093
00094 ScbXml xml;
00095 if (RET_OK != ermXmlOpenFile(ptr->scbname, &xml.handle))
00096 {
00097 SCB_ERROR("Could not open the file %s!", ptr->scbname);
00098 return NULL;
00099 }
00100 SCB_TRACE("Successfully open file %s", ptr->scbname);
00101
00102
00103 ScbDocPtr doc = scb_doc_new();
00104 if (NULL == doc)
00105 {
00106 ermXmlClose(&xml.handle);
00107 return NULL;
00108 }
00109 memcpy(&doc->path, ptr, sizeof(doc->path));
00110 SCB_TRACE("Successfully construct document object!");
00111
00112
00113 strncpy(xml.xPath, "/notes", SCB_MAX_XML_PATH);
00114 xml.index = 1;
00115
00116
00117 if (scb_pages_load(&doc->pages, &xml))
00118 {
00119 ermXmlClose(&xml.handle);
00120 SCB_TRACE("Successfully load all pages!");
00121 return doc;
00122 }
00123 else
00124 {
00125 ermXmlClose(&xml.handle);
00126 scb_doc_free(doc);
00127 SCB_TRACE("Error occurs during loading pages!");
00128 return NULL;
00129 }
00130 }
00131
00132
00133
00134 ScbDocPtr scb_doc_make_sure_exist(ScbPathPtr ptr)
00135 {
00136 if (!_doc_check_path(ptr)) return NULL;
00137 ScbDocPtr doc = NULL;
00138
00139
00140 if (scb_path_file_exist(ptr->scbname))
00141 {
00142 doc = scb_doc_open(ptr);
00143 if (doc)
00144 {
00145 return doc;
00146 }
00147 }
00148
00149
00150
00151 doc = scb_doc_new();
00152 if (NULL == doc)
00153 {
00154
00155 return NULL;
00156 }
00157 if (scb_doc_saveAs(doc, ptr))
00158 {
00159 return doc;
00160 }
00161 scb_doc_free(doc);
00162 return NULL;
00163 }
00164
00165
00166
00167 gboolean scb_doc_save(ScbDocPtr doc)
00168 {
00169 SCB_RET_FALSE_IF(NULL == doc, "Invalid doc pointer!");
00170
00171
00172 ScbXml xml;
00173 if (RET_OK != ermXmlCreateFile(doc->path.scbname, &xml.handle))
00174 {
00175 SCB_ERROR("Could not create the file %s!", doc->path.scbname);
00176 return FALSE;
00177 }
00178 SCB_TRACE("Successfully create file %s", doc->path.scbname);
00179
00180
00181 ermXmlNewString(&xml.handle, "/" , "notes", "");
00182 SCB_TRACE("Create root node scbdoc");
00183
00184
00185 ermXmlNewString(&xml.handle, "/notes", "version", "");
00186 ermXmlNewString(&xml.handle, "/notes/version", "number", SCB_LIB_VERSION);
00187 ermXmlNewString(&xml.handle, "/notes/version", "organization", SCB_LIB_ORG);
00188
00189
00190 ermXmlNewString(&xml.handle, "/notes", "screen", "");
00191 ermXmlNewString(&xml.handle, "/notes/screen", "units", SCB_DEF_SCREEN_UNITS);
00192 ermXmlNewInt(&xml.handle, "/notes/screen", "dpi", SCB_DEF_SCREEN_DPI);
00193
00194
00195 strncpy(xml.xPath, "/notes", SCB_MAX_XML_PATH);
00196 xml.index = 0;
00197
00198
00199 if (scb_pages_save(&doc->pages, &xml))
00200 {
00201 ermXmlSaveAndClose(&xml.handle);
00202 SCB_TRACE("Successfully save all pages!");
00203 return TRUE;
00204 }
00205 else
00206 {
00207 ermXmlClose(&xml.handle);
00208 SCB_TRACE("Error occurs during saving pages!");
00209 return FALSE;
00210 }
00211 }
00212
00213
00214
00215 gboolean scb_doc_saveAs(ScbDocPtr doc, ScbPathPtr ptr)
00216 {
00217 SCB_RET_FALSE_IF(NULL == doc || NULL == ptr, "Invalid pointer(s)!");
00218 memcpy(&doc->path, ptr, sizeof(doc->path));
00219 return scb_doc_save(doc);
00220 }
00221
00222
00223
00224 ScbPagesPtr scb_doc_get_pages(ScbDocPtr ptr)
00225 {
00226 if (ptr)
00227 {
00228 return &ptr->pages;
00229 }
00230 return NULL;
00231 }
00232
00233 ScbStrokeStylePtr scb_doc_get_current_stroke_style(ScbDocPtr doc)
00234 {
00235 SCB_RET_NULL_IF(NULL == doc, "Invalid pointer!");
00236 return &doc->context.curStrokeStyle;
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 ScbPagePtr scb_doc_get_page(ScbDocPtr doc, ScbPageIdPtr id)
00271 {
00272 ScbPagesPtr pages = scb_doc_get_pages(doc);
00273 if (NULL == pages) return NULL;
00274 return scb_pages_get_page(pages, id);
00275 }
00276
00277
00278 gboolean scb_doc_add_page(ScbDocPtr doc, ScbPagePtr page)
00279 {
00280 ScbPagesPtr pages = scb_doc_get_pages(doc);
00281 if (NULL == pages) return FALSE;
00282 return scb_pages_add_page(pages, page);
00283 }
00284
00285 void scb_doc_init_context(ScbDocPtr ptr)
00286 {
00287 SCB_RET_IF(NULL == ptr, "Invalid doc pointer!");
00288 ptr->context.table = g_array_sized_new(FALSE, TRUE,
00289 sizeof(ScbTBSItem), SCB_DEF_CMD_ACT_SIZE);
00290 ptr->context.curState = SCB_TBS_SCRIBBLE;
00291 ptr->context.curStrokeStyle.color = SCB_DEV_COLOR_BLACK;
00292 ptr->context.curStrokeStyle.layer = SCB_DEF_STROKE_LAYER;
00293 ptr->context.curStrokeStyle.lineStyle = SCB_DEF_STROKE_LINESTYLE;
00294 ptr->context.curStrokeStyle.penSize = SCB_DEF_STROKE_PENSIZE;
00295
00296 ptr->context.appId = -1;
00297 }
00298
00299 void scb_doc_free_context(ScbDocPtr ptr)
00300 {
00301 SCB_RET_IF(NULL == ptr, "Invalid doc pointer!");
00302 g_array_free(ptr->context.table, TRUE);
00303 ptr->context.table = NULL;
00304 }
00305
00306 void scb_doc_add_map_item(ScbDocPtr doc, const ScbTBSItemPtr ptr)
00307 {
00308 SCB_RET_IF(NULL == doc || NULL == ptr, "Invalid pointer(s)!");
00309 g_array_append_val(doc->context.table, *ptr);
00310 }
00311
00312 ScbTBState scb_doc_get_current_state(ScbDocPtr doc)
00313 {
00314 if (NULL == doc) return SCB_TBS_INVALID;
00315 return doc->context.curState;
00316 }
00317
00318 gboolean scb_state_is_scribble(ScbDocPtr doc)
00319 {
00320 return SCB_TBS_SCRIBBLE == scb_doc_get_current_state(doc);
00321 }
00322 gboolean scb_state_is_erase(ScbDocPtr doc)
00323 {
00324 return SCB_TBS_ERASE == scb_doc_get_current_state(doc);
00325 }
00326
00327
00328
00329 void scb_doc_dump(ScbDocPtr ptr)
00330 {
00331 if (ptr)
00332 {
00333 scb_pages_dump(&ptr->pages);
00334 }
00335 }
00336
00337
00338