Tizen 2.0 Release
[samples/web/CallLog.git] / js / app.model.js
1 /*jslint devel: true*/
2 /*global tizen, $, app, localStorage */
3
4 /**
5  * @class Model
6  */
7 function Model() {
8         'use strict';
9
10         this.init();
11 }
12
13 (function () { // strict mode wrapper
14         'use strict';
15         Model.prototype = {
16
17                 /**
18                  * API module initialisation
19                  */
20                 init: function Model_init() {
21                 },
22
23                 /**
24                  * Registers listener for call history change
25                  * @param {onSuccessCallback} function
26                  */
27                 registerChangeListener: function Model_registerChangeListener(onSuccessCallback) {
28
29                         var callHistoryListener = {
30                                 onadded: onSuccessCallback,
31                                 onchanged: onSuccessCallback
32                         };
33
34                         tizen.callhistory.addChangeListener(callHistoryListener);
35                 },
36
37                 /**
38                  * @param {function} onSuccess callback
39                  * @param {function} onError callback
40                  */
41                 getCallHistory: function Model_getCallHistory(onSuccess, onError) {
42                         if (typeof onError !== 'function') {
43                                 onError = function (e) {
44                                         console.error('Model_getCallHistory error', e);
45                                 };
46                         }
47
48                         tizen.callhistory.find(onSuccess, onError, null, new tizen.SortMode('startTime', 'DESC'));
49                 },
50
51                 /**
52                  * @param {number} phoneNumber
53                  * @param {function} onSuccess Callback
54                  */
55                 getCallHistoryForCaller: function Model_getCallHistoryForCaller(phoneNumber, onSuccess) {
56
57                         tizen.callhistory.find(onSuccess, undefined, // error is ignored
58                                 new tizen.AttributeFilter('remoteParties.remoteParty', 'EXACTLY', phoneNumber),
59                                 new tizen.SortMode('startTime', 'DESC')
60                                 );
61                 },
62
63                 /**
64                  * Deletes specified log entry
65                  * @param {CallHistoryEntry} entry
66                  */
67                 deleteLog: function Model_deleteLog(entry) {
68                         try {
69                                 tizen.callhistory.remove(entry);
70                         } catch (e) {
71                                 console.error('Error on entry delete: ' + e.message);
72                         }
73                 },
74
75                 /**
76                  * @param {number} addressBookId
77                  * @param {number} contactId
78                  * @returns {string} photoURI
79                  */
80                 getPhotoURIForContact: function Model_getPhotoURIForContact(addressBookId, contactId) {
81                         try {
82                                 var addressBook = tizen.contact.getAddressBook(addressBookId),
83                                         contact;
84
85                                 if (addressBook) {
86                                         contact = addressBook.get(contactId);
87                                         return contact.photoURI || false;
88                                 }
89                         } catch (e) {
90                                 console.error('updatePhotoByContactId error:' + e.message);
91                         }
92                         return false;
93                 }
94
95         };
96 }());