Code Sync with Private GIT
[apps/web/download-manager.git] / src / main.cpp
1 /*
2  * Copyright 2012  Samsung Electronics Co., Ltd
3  *
4  * Licensed under the Flora License, Version 1.0 (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://www.tizenopensource.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 /**
18  * @file        main.cpp
19  * @author      Jungki Kwak (jungki.kwak@samsung.com)
20  * @brief       main file for download manager
21  */
22
23 #include <list>
24 #include <string>
25 #include <iostream>
26 #include <memory>
27
28 #include "Ecore_X.h"
29 #include "aul.h"
30 #include "app.h"
31 #include "app_service.h"
32
33 #include "download-manager-common.h"
34 #include "download-manager-view.h"
35 #include "download-manager-network.h"
36 #include "download-manager-downloadRequest.h"
37 #include "download-manager-history-db.h"
38
39 using namespace std;
40
41 struct app_data_t {
42         Ecore_Idler *idler;
43         int history_count;
44         int load_count;
45 };
46
47 #ifndef _TIZEN_PUBLIC
48 int __get_rotate_angle()
49 {
50         int angle = 0;
51         app_device_orientation_e rotation_state;
52         rotation_state = app_get_device_orientation();
53
54         DP_LOG("Rotate angle[%d]",rotation_state);
55         return rotation_state;
56 }
57
58 static void __rotate_changed_cb(app_device_orientation_e m, void *data)
59 {
60         int angle = 0;
61         DownloadView &view = DownloadView::getInstance();
62         angle = __get_rotate_angle();
63         view.rotateWindow(angle);
64         return;
65 }
66 #endif
67
68 static void __lang_changed_cb(void *data)
69 {
70         DP_LOG("=== Language changed nofification ===");
71         return;
72 }
73
74 static void __region_changed_cb(void *data)
75 {
76         DownloadView &view = DownloadView::getInstance();
77         view.changedRegion();
78         DP_LOG("=== Region changed nofification ===");
79         return;
80 }
81
82 static void __low_memory_cb(void *data)
83 {
84         DP_LOG("=== Low memory nofification ===");
85         return;
86 }
87
88 static Eina_Bool __load_remained_history(void *data)
89 {
90         struct app_data_t *app_data = (struct app_data_t *)data;
91         if (app_data && app_data->load_count <= app_data->history_count) {
92                 app_data->load_count += LOAD_HISTORY_COUNT;
93                 DownloadHistoryDB::createRemainedItemsFromHistoryDB(
94                         LOAD_HISTORY_COUNT, app_data->load_count);
95                 return ECORE_CALLBACK_RENEW;
96         } else
97                 return ECORE_CALLBACK_CANCEL;
98 }
99
100 static bool __app_create(void *data)
101 {
102         int count = 0;
103 #ifndef _TIZEN_PUBLIC
104         int angle = 0;
105 #endif
106         struct app_data_t *app_data = (struct app_data_t *)data;
107         DP_LOG_START("App Create");
108
109         DownloadView &view = DownloadView::getInstance();
110         Evas_Object *window = view.create();
111         if (!window) {
112                 DP_LOGE("Fail to create main window");
113                 return false;
114         }
115         /* Init network */
116         NetMgr &netObj = NetMgr::getInstance();
117         netObj.initNetwork();
118
119 #ifndef _TIZEN_PUBLIC
120         angle = __get_rotate_angle();
121         view.rotateWindow(angle);
122 #endif
123 #ifndef _SILENT_LAUNCH
124         evas_object_show(window);
125         view.show();
126 #endif
127
128         DownloadHistoryDB::getCountOfHistory(&count);
129         if (count > 0) {
130                 DownloadHistoryDB::createItemsFromHistoryDB();
131                 if (count > LOAD_HISTORY_COUNT) {
132                         if (app_data) {
133                                 app_data->history_count = count;
134                                 app_data->idler = ecore_idler_add(__load_remained_history, app_data);
135                         }
136                 }
137         }
138
139         DP_LOG_END("App Create");
140
141         return true;
142 }
143
144 static void __app_terminate(void *data)
145 {
146         DP_LOG_FUNC();
147         struct app_data_t *app_data = (struct app_data_t *)data;
148         NetMgr &netObj = NetMgr::getInstance();
149         netObj.deinitNetwork();
150         DownloadView &view = DownloadView::getInstance();
151         view.destroy();
152         if (app_data && app_data->idler)
153                 ecore_idler_del(app_data->idler);
154         if (app_data) {
155                 free(app_data);
156                 app_data = NULL;
157         }
158         return;
159 }
160
161 static void __app_pause(void *data)
162 {
163         DP_LOG_FUNC();
164         return;
165 }
166
167 static void __app_resume(void *data)
168 {
169         return;
170 }
171
172 static void __app_service(service_h s, void *data)
173 {
174         string s_url = std::string();
175         string s_cookie = std::string();
176         char *url = NULL;
177         char *cookie = NULL;
178         char *mode = NULL;
179         char *app_op = NULL;
180         DownloadView &view = DownloadView::getInstance();
181
182         DP_LOG_FUNC();
183
184         if (service_get_operation(s, &app_op) < 0) {
185                 DP_LOGE("Fail to get service operation");
186                 return;
187         }
188         DP_LOG("operation[%s]", app_op);
189
190         if (service_get_uri(s, &url) < 0) {
191                 DP_LOGE("Invalid URL");
192         } else {
193                 DP_LOG("url[%s]",url);
194                 if (url)
195                         s_url = url;
196         }
197
198         if (service_get_extra_data(s, "cookie", &cookie) < 0) {
199                 DP_LOG("No cookie");
200         } else {
201                 DP_LOG("cookie[%s]",cookie);
202                 if (cookie)
203                         s_cookie = cookie;
204         }
205
206         if (service_get_extra_data(s, "mode", &mode) < 0) {
207                 DP_LOG("No mode");
208         } else {
209                 DP_LOG("mode[%s]",mode);
210                 if ( 0 == strncmp(mode, "view", strlen("view"))) {
211                         DP_LOG("View mode");
212                         view.activateWindow();
213                         return;
214                 }
215                 DP_LOGE("Invalid mode");
216                 view.activateWindow();
217                 return;
218         }
219
220         if (s_url.empty()) {
221                 view.activateWindow();
222                 return;
223         }
224         DownloadRequest request(s_url, s_cookie);
225         Item::create(request);
226 #ifndef _SILENT_LAUNCH
227         view.activateWindow();
228 #endif
229
230         return;
231 }
232
233 int main(int argc, char *argv[])
234 {
235         app_event_callback_s evt_cb = {0,};
236         int ret = 0;
237         struct app_data_t *app_data = NULL;
238
239         app_data = (struct app_data_t *)calloc(1, sizeof(struct app_data_t));
240         if (!app_data) {
241                 DP_LOGE("Fail to calloc of app data");
242                 return ret;
243         }
244
245         evt_cb.create = __app_create;
246         evt_cb.terminate = __app_terminate;
247         evt_cb.pause = __app_pause;
248         evt_cb.resume = __app_resume;
249         evt_cb.service = __app_service;
250         evt_cb.low_memory = __low_memory_cb;
251         evt_cb.low_battery = NULL;
252 #ifndef _TIZEN_PUBLIC
253         evt_cb.device_orientation = __rotate_changed_cb;
254 #else
255         evt_cb.device_orientation = NULL;
256 #endif
257         evt_cb.language_changed = __lang_changed_cb;
258         evt_cb.region_format_changed = __region_changed_cb;
259
260         ret = app_efl_main(&argc, &argv, &evt_cb, app_data);
261         DP_LOG("Main return");
262
263         return ret;
264 }
265