Updated application sources
[apps/web/sample/CallLog.git] / project / js / app.model.js
1 /*
2 *      Copyright 2013  Samsung Electronics Co., Ltd
3 *
4 *      Licensed under the Flora License, Version 1.1 (the "License");
5 *      you may not use this file except in compliance with the License.
6 *      You may obtain a copy of the License at
7 *
8 *              http://floralicense.org/license/
9 *
10 *      Unless required by applicable law or agreed to in writing, software
11 *      distributed under the License is distributed on an "AS IS" BASIS,
12 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *      See the License for the specific language governing permissions and
14 *      limitations under the License.
15 */
16
17 /*jslint devel: true*/
18 /*global tizen, $, app, localStorage */
19
20 /**
21 * @class Model
22 */
23 function Model() {
24     'use strict';
25
26     this.init();
27 }
28
29 (function () { // strict mode wrapper
30     'use strict';
31
32     Model.prototype = {
33
34         /**
35         * API module initialisation
36         */
37         init: function Model_init() {
38         },
39
40         /**
41         * Registers listener for call history change
42         * @param {onSuccessCallback} function
43         */
44         registerChangeListener: function Model_registerChangeListener(
45             onSuccessCallback
46         ) {
47             var callHistoryListener = {
48                 onadded: onSuccessCallback,
49                 onchanged: onSuccessCallback,
50                 onremoved: onSuccessCallback
51             };
52             tizen.callhistory.addChangeListener(callHistoryListener);
53         },
54
55         /**
56         * @param {function} onSuccess callback
57         * @param {function} onError callback
58         */
59         getCallHistory: function Model_getCallHistory(onSuccess, onError) {
60             if (typeof onError !== 'function') {
61                 onError = function (e) {
62                     console.error('Model_getCallHistory error', e);
63                 };
64             }
65
66             tizen.callhistory.find(
67                 onSuccess,
68                 onError,
69                 null,
70                 new tizen.SortMode('startTime', 'DESC')
71             );
72         },
73
74         /**
75         * @param {number} phoneNumber
76         * @param {function} onSuccess Callback
77         */
78         getCallHistoryForCaller: function Model_getCallHistoryForCaller(
79             phoneNumber,
80             onSuccess
81         ) {
82             var filter = null, success;
83             if (phoneNumber) {
84                 filter = new tizen.AttributeFilter(
85                     'remoteParties.remoteParty',
86                     'EXACTLY',
87                     phoneNumber
88                 );
89                 success = onSuccess;
90             } else {
91                 success = function (calls) {
92                     onSuccess(calls.filter(function (element) {
93                         if (!element.remoteParties[0].remoteParty) {
94                             return element;
95                         }
96                     }));
97                 };
98             }
99
100             tizen.callhistory.find(success,
101                 function (e) {
102                     console.error(e);
103                 },
104                 filter,
105                 new tizen.SortMode('startTime', 'DESC')
106                 );
107         },
108
109         /**
110         * Deletes specified log entry
111         * @param {CallHistoryEntry} entry
112         */
113         deleteLog: function Model_deleteLog(entry) {
114             try {
115                 tizen.callhistory.remove(entry);
116             } catch (e) {
117                 console.error('Error on entry delete: ' + e.message);
118             }
119         },
120
121         /**
122         * @param {number} addressBookId
123         * @param {number} contactId
124         * @returns {string} photoURI
125         */
126         getPhotoURIForContact: function Model_getPhotoURIForContact(personId) {
127             try {
128                 var addressBook = tizen.contact.getDefaultAddressBook(),
129                     contact;
130                 if (addressBook) {
131                     contact = addressBook.get(personId);
132                     return contact.photoURI || false;
133                 }
134             } catch (e) {
135                 console.error('updatePhotoByContactId error:' + e.message);
136             }
137             return false;
138         }
139
140     };
141
142 }());