Revert "Updated application sources"
[apps/web/sample/CallLog.git] / project / 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                                 onremoved: onSuccessCallback
33                         };
34
35                         tizen.callhistory.addChangeListener(callHistoryListener);
36                 },
37
38                 /**
39                  * @param {function} onSuccess callback
40                  * @param {function} onError callback
41                  */
42                 getCallHistory: function Model_getCallHistory(onSuccess, onError) {
43                         if (typeof onError !== 'function') {
44                                 onError = function (e) {
45                                         console.error('Model_getCallHistory error', e);
46                                 };
47                         }
48
49                         tizen.callhistory.find(onSuccess, onError, null, new tizen.SortMode('startTime', 'DESC'));
50                 },
51
52                 /**
53                  * @param {number} phoneNumber
54                  * @param {function} onSuccess Callback
55                  */
56                 getCallHistoryForCaller: function Model_getCallHistoryForCaller(phoneNumber, onSuccess) {
57                         var filter = null, success;
58                         if (phoneNumber) {
59                                 filter = new tizen.AttributeFilter('remoteParties.remoteParty', 'EXACTLY', phoneNumber);
60                                 success = onSuccess;
61                         } else {
62                                 success = function (calls) {
63                                         onSuccess(calls.filter(function (element) {
64                                                 if(!element.remoteParties[0].remoteParty) {
65                                                         return element;
66                                                 }
67                                         }));
68                                 };
69                         }
70
71                         tizen.callhistory.find(success,
72                                         function (e) {
73                                                 console.error(e);
74                                         },
75                                         filter,
76                                         new tizen.SortMode('startTime', 'DESC')
77                                         );
78                 },
79
80                 /**
81                  * Deletes specified log entry
82                  * @param {CallHistoryEntry} entry
83                  */
84                 deleteLog: function Model_deleteLog(entry) {
85                         try {
86                                 tizen.callhistory.remove(entry);
87                         } catch (e) {
88                                 console.error('Error on entry delete: ' + e.message);
89                         }
90                 },
91
92                 /**
93                  * @param {number} addressBookId
94                  * @param {number} contactId
95                  * @returns {string} photoURI
96                  */
97                 getPhotoURIForContact: function Model_getPhotoURIForContact(personId) {
98                         try {
99                                 var addressBook = tizen.contact.getDefaultAddressBook(),
100                                         contact;
101
102                                 if (addressBook) {
103                                         contact = addressBook.get(personId);
104                                         return contact.photoURI || false;
105                                 }
106                         } catch (e) {
107                                 console.error('updatePhotoByContactId error:' + e.message);
108                         }
109                         return false;
110                 }
111
112         };
113 }());