Tizen 2.1 base
[platform/framework/web/download-provider.git] / src / download-provider-request.c
1 /*
2  * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
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 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <time.h>
21 #include <sys/time.h>
22
23 #include "download-provider.h"
24 #include "download-provider-log.h"
25
26 #include "download-provider-slots.h"
27 #include "download-provider-socket.h"
28 #include "download-provider-db.h"
29 #include "download-provider-pthread.h"
30
31
32 ///////// below functions are called by main thread of thread-request.c
33
34
35
36 //////////////////////////////////////////////////////////////////////////
37 /// @brief              create unique id as integer type
38 /// @return     unique id combined local time and the special calculation
39 static int __get_download_request_id(void)
40 {
41         int uniquetime = 0;
42         struct timeval tval;
43         static int last_uniquetime = 0;
44
45         do {
46                 uniquetime = (int)time(NULL);
47                 gettimeofday(&tval, NULL);
48                 if (tval.tv_usec == 0)
49                         uniquetime = uniquetime + (tval.tv_usec + 1) % 0xfffff;
50                 else
51                         uniquetime = uniquetime + tval.tv_usec;
52                 TRACE_INFO("ID : %d", uniquetime);
53         } while (last_uniquetime == uniquetime);
54         last_uniquetime = uniquetime;   // store
55         return uniquetime;
56 }
57
58 char *dp_print_state(dp_state_type state)
59 {
60         switch(state)
61         {
62                 case DP_STATE_NONE :
63                         return "NONE";
64                 case DP_STATE_READY :
65                         return "READY";
66                 case DP_STATE_QUEUED :
67                         return "QUEUED";
68                 case DP_STATE_CONNECTING :
69                         return "CONNECTING";
70                 case DP_STATE_DOWNLOADING :
71                         return "DOWNLOADING";
72                 case DP_STATE_PAUSE_REQUESTED :
73                         return "PAUSE_REQUESTED";
74                 case DP_STATE_PAUSED :
75                         return "PAUSED";
76                 case DP_STATE_COMPLETED :
77                         return "COMPLETED";
78                 case DP_STATE_CANCELED :
79                         return "CANCELED";
80                 case DP_STATE_FAILED :
81                         return "FAILED";
82                 default :
83                         break;
84         }
85         return "UNKNOWN";
86 }
87
88 char *dp_print_errorcode(dp_error_type errorcode)
89 {
90         switch(errorcode)
91         {
92                 case DP_ERROR_NONE :
93                         return "NONE";
94                 case DP_ERROR_INVALID_PARAMETER :
95                         return "INVALID_PARAMETER";
96                 case DP_ERROR_OUT_OF_MEMORY :
97                         return "OUT_OF_MEMORY";
98                 case DP_ERROR_IO_ERROR :
99                         return "IO_ERROR";
100                 case DP_ERROR_NETWORK_UNREACHABLE :
101                         return "NETWORK_UNREACHABLE";
102                 case DP_ERROR_CONNECTION_TIMED_OUT :
103                         return "CONNECTION_TIMED_OUT";
104                 case DP_ERROR_NO_SPACE :
105                         return "NO_SPACE";
106                 case DP_ERROR_FIELD_NOT_FOUND :
107                         return "FIELD_NOT_FOUND";
108                 case DP_ERROR_INVALID_STATE :
109                         return "INVALID_STATE";
110                 case DP_ERROR_CONNECTION_FAILED :
111                         return "CONNECTION_FAILED";
112                 case DP_ERROR_INVALID_URL :
113                         return "INVALID_URL";
114                 case DP_ERROR_INVALID_DESTINATION :
115                         return "INVALID_DESTINATION";
116                 case DP_ERROR_QUEUE_FULL :
117                         return "QUEUE_FULL";
118                 case DP_ERROR_ALREADY_COMPLETED :
119                         return "ALREADY_COMPLETED";
120                 case DP_ERROR_FILE_ALREADY_EXISTS :
121                         return "FILE_ALREADY_EXISTS";
122                 case DP_ERROR_TOO_MANY_DOWNLOADS :
123                         return "TOO_MANY_DOWNLOADS";
124                 case DP_ERROR_NO_DATA :
125                         return "NO_DATA";
126                 case DP_ERROR_UNHANDLED_HTTP_CODE :
127                         return "UNHANDLED_HTTP_CODE";
128                 case DP_ERROR_CANNOT_RESUME :
129                         return "CANNOT_RESUME";
130                 case DP_ERROR_RESPONSE_TIMEOUT :
131                         return "RESPONSE_TIMEOUT";
132                 case DP_ERROR_REQUEST_TIMEOUT :
133                         return "REQUEST_TIMEOUT";
134                 case DP_ERROR_SYSTEM_DOWN :
135                         return "SYSTEM_DOWN";
136                 case DP_ERROR_CLIENT_DOWN :
137                         return "CLIENT_DOWN";
138                 case DP_ERROR_ID_NOT_FOUND :
139                         return "ID_NOT_FOUND";
140                 default :
141                         break;
142         }
143         return "UNKNOWN";
144 }
145
146 char *dp_strdup(char *src)
147 {
148         char *dest = NULL;
149         size_t src_len = 0;
150
151         if (src == NULL) {
152                 TRACE_ERROR("[CHECK PARAM]");
153                 return NULL;
154         }
155
156         src_len = strlen(src);
157         if (src_len <= 0) {
158                 TRACE_ERROR("[CHECK PARAM] len[%d]", src_len);
159                 return NULL;
160         }
161
162         dest = (char *)calloc(src_len + 1, sizeof(char));
163         if (dest == NULL) {
164                 TRACE_STRERROR("[CHECK] allocation");
165                 return NULL;
166         }
167         memcpy(dest, src, src_len * sizeof(char));
168         dest[src_len] = '\0';
169
170         return dest;
171 }
172
173 // check param
174 // create new slot
175 // fill info to new slot
176 // make new id
177 // save info to QUEUE(DB)
178 dp_error_type dp_request_create(int id, dp_client_group *group, dp_request **empty_slot)
179 {
180         if (id != -1) {
181                 TRACE_ERROR("[CHECK PROTOCOL] ID not -1");
182                 return DP_ERROR_INVALID_STATE;
183         }
184         if (!group || !empty_slot) {
185                 TRACE_ERROR("[CHECK INTERNAL][%d]", id);
186                 return DP_ERROR_IO_ERROR;
187         }
188         // New allocation Slot
189         dp_request *new_request = dp_request_new();
190         if (!new_request) {
191                 TRACE_STRERROR("[CHECK MEMORY][%d]", id);
192                 return DP_ERROR_OUT_OF_MEMORY;
193         }
194
195         new_request->id = __get_download_request_id();
196         new_request->group = group;
197         if (group->pkgname && strlen(group->pkgname) > 1)
198                 new_request->packagename = dp_strdup(group->pkgname);
199         new_request->credential = group->credential;
200         if (new_request->packagename == NULL) {
201                 dp_request_free(new_request);
202                 TRACE_ERROR("[ERROR][%d] OUT_OF_MEMORY [PACKAGENAME]", id);
203                 return DP_ERROR_OUT_OF_MEMORY;
204         }
205         new_request->state = DP_STATE_READY;
206         new_request->error = DP_ERROR_NONE;
207         if (dp_db_insert_column
208                         (new_request->id, DP_DB_TABLE_LOG, DP_DB_COL_STATE,
209                         DP_DB_COL_TYPE_INT, &new_request->state) < 0) {
210                 TRACE_ERROR("[CHECK SQL][%d]", id);
211                 dp_request_free(new_request);
212                 return DP_ERROR_OUT_OF_MEMORY;
213         }
214         if (dp_db_set_column
215                         (new_request->id, DP_DB_TABLE_LOG, DP_DB_COL_PACKAGENAME,
216                         DP_DB_COL_TYPE_TEXT, new_request->packagename) < 0)
217                 TRACE_ERROR("[CHECK SQL][%d]", id);
218         if (dp_db_update_date
219                         (new_request->id, DP_DB_TABLE_LOG, DP_DB_COL_CREATE_TIME) < 0)
220                 TRACE_ERROR("[CHECK SQL][%d]", id);
221
222         new_request->create_time = (int)time(NULL);
223
224         *empty_slot = new_request;
225         return DP_ERROR_NONE;
226 }
227
228 dp_error_type dp_request_set_url(int id, dp_request *request, char *url)
229 {
230         int length = 0;
231         if (!url || (length = strlen(url)) <= 1)
232                 return DP_ERROR_INVALID_URL;
233
234         if (request != NULL) {
235                 if (request->state == DP_STATE_CONNECTING ||
236                         request->state == DP_STATE_DOWNLOADING ||
237                         request->state == DP_STATE_COMPLETED) {
238                         TRACE_ERROR
239                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
240                         return DP_ERROR_INVALID_STATE;
241                 }
242         } else {
243                 // check id in logging table.
244                 dp_state_type state =
245                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
246
247                 if (state <= DP_STATE_NONE) {
248                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
249                         return DP_ERROR_ID_NOT_FOUND;
250                 }
251                 // check again from logging table
252                 if (state == DP_STATE_CONNECTING ||
253                         state == DP_STATE_DOWNLOADING ||
254                         state == DP_STATE_COMPLETED) {
255                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
256                         return DP_ERROR_INVALID_STATE;
257                 }
258         }
259
260         if (dp_db_replace_column
261                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_URL,
262                         DP_DB_COL_TYPE_TEXT, url) < 0) {
263                 TRACE_ERROR("[CHECK SQL][%d]", id);
264                 return DP_ERROR_IO_ERROR;
265         }
266         return DP_ERROR_NONE;
267 }
268
269 dp_error_type dp_request_set_destination(int id, dp_request *request, char *dest)
270 {
271         int length = 0;
272         if (!dest || (length = strlen(dest)) <= 1)
273                 return DP_ERROR_INVALID_DESTINATION;
274
275         if (request != NULL) {
276                 if (request->state == DP_STATE_CONNECTING ||
277                         request->state == DP_STATE_DOWNLOADING ||
278                         request->state == DP_STATE_COMPLETED) {
279                         TRACE_ERROR
280                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
281                         return DP_ERROR_INVALID_STATE;
282                 }
283         } else {
284                 // check id in logging table.
285                 dp_state_type state =
286                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
287
288                 if (state <= DP_STATE_NONE) {
289                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
290                         return DP_ERROR_ID_NOT_FOUND;
291                 }
292                 // check again from logging table
293                 if (state == DP_STATE_CONNECTING ||
294                         state == DP_STATE_DOWNLOADING ||
295                         state == DP_STATE_COMPLETED) {
296                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
297                         return DP_ERROR_INVALID_STATE;
298                 }
299         }
300
301         if (dp_db_replace_column
302                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_DESTINATION,
303                         DP_DB_COL_TYPE_TEXT, dest) < 0) {
304                 TRACE_ERROR("[CHECK SQL][%d]", id);
305                 return DP_ERROR_IO_ERROR;
306         }
307         return DP_ERROR_NONE;
308 }
309
310 dp_error_type dp_request_set_filename(int id, dp_request *request, char *filename)
311 {
312         int length = 0;
313         if (!filename || (length = strlen(filename)) <= 1)
314                 return DP_ERROR_INVALID_PARAMETER;
315
316         if (request != NULL) {
317                 if (request->state == DP_STATE_CONNECTING ||
318                         request->state == DP_STATE_DOWNLOADING ||
319                         request->state == DP_STATE_COMPLETED) {
320                         TRACE_ERROR
321                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
322                         return DP_ERROR_INVALID_STATE;
323                 }
324         } else {
325                 // check id in logging table.
326                 dp_state_type state =
327                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
328
329                 if (state <= DP_STATE_NONE) {
330                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
331                         return DP_ERROR_ID_NOT_FOUND;
332                 }
333                 // check again from logging table
334                 if (state == DP_STATE_CONNECTING ||
335                         state == DP_STATE_DOWNLOADING ||
336                         state == DP_STATE_COMPLETED) {
337                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
338                         return DP_ERROR_INVALID_STATE;
339                 }
340         }
341
342         if (dp_db_replace_column
343                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_FILENAME,
344                         DP_DB_COL_TYPE_TEXT, filename) < 0) {
345                 TRACE_ERROR("[CHECK SQL][%d]", id);
346                 return DP_ERROR_IO_ERROR;
347         }
348
349         TRACE_INFO("ID [%d] Filename[%s]", id, filename);
350         return DP_ERROR_NONE;
351 }
352
353 dp_error_type dp_request_set_notification(int id, dp_request *request, unsigned enable)
354 {
355         if (request != NULL) {
356                 if (request->state == DP_STATE_COMPLETED) {
357                         TRACE_ERROR
358                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
359                         return DP_ERROR_INVALID_STATE;
360                 }
361         } else {
362                 // check id in logging table.
363                 dp_state_type state =
364                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
365
366                 if (state <= DP_STATE_NONE) {
367                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
368                         return DP_ERROR_ID_NOT_FOUND;
369                 }
370                 // check again from logging table
371                 if (state == DP_STATE_COMPLETED) {
372                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
373                         return DP_ERROR_INVALID_STATE;
374                 }
375         }
376
377         // update queue DB
378         if (dp_db_replace_column
379                         (id, DP_DB_TABLE_NOTIFICATION,
380                         DP_DB_COL_NOTIFICATION_ENABLE, DP_DB_COL_TYPE_INT,
381                         &enable) < 0) {
382                 TRACE_ERROR("[CHECK SQL][%d]", id);
383                 return DP_ERROR_IO_ERROR;
384         }
385         // update memory
386         if (request)
387                 request->auto_notification = enable;
388         return DP_ERROR_NONE;
389 }
390
391 dp_error_type dp_request_set_auto_download(int id, dp_request *request, unsigned enable)
392 {
393         if (request != NULL) {
394                 if (request->state == DP_STATE_COMPLETED) {
395                         TRACE_ERROR
396                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
397                         return DP_ERROR_INVALID_STATE;
398                 }
399         } else {
400                 // check id in logging table.
401                 dp_state_type state =
402                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
403
404                 if (state <= DP_STATE_NONE) {
405                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
406                         return DP_ERROR_ID_NOT_FOUND;
407                 }
408                 // check again from logging table
409                 if (state == DP_STATE_COMPLETED) {
410                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
411                         return DP_ERROR_INVALID_STATE;
412                 }
413         }
414
415         // update queue DB
416         if (dp_db_replace_column
417                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_AUTO_DOWNLOAD,
418                         DP_DB_COL_TYPE_INT, &enable) < 0) {
419                 TRACE_ERROR("[CHECK SQL][%d]", id);
420                 return DP_ERROR_IO_ERROR;
421         }
422         return DP_ERROR_NONE;
423 }
424
425 dp_error_type dp_request_set_state_event(int id, dp_request *request, unsigned enable)
426 {
427         if (request == NULL) {
428                 // check id in logging table.
429                 dp_state_type state =
430                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
431
432                 if (state <= DP_STATE_NONE) {
433                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
434                         return DP_ERROR_ID_NOT_FOUND;
435                 }
436         }
437
438         // update queue DB
439         if (dp_db_replace_column
440                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_STATE_EVENT,
441                         DP_DB_COL_TYPE_INT, &enable) < 0) {
442                 TRACE_ERROR("[CHECK SQL][%d]", id);
443                 return DP_ERROR_IO_ERROR;
444         }
445         // update memory
446         if (request)
447                 request->state_cb = enable;
448         return DP_ERROR_NONE;
449 }
450
451 dp_error_type dp_request_set_progress_event(int id, dp_request *request, unsigned enable)
452 {
453         if (request == NULL) {
454                 // check id in logging table.
455                 dp_state_type state =
456                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
457
458                 if (state <= DP_STATE_NONE) {
459                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
460                         return DP_ERROR_ID_NOT_FOUND;
461                 }
462         }
463
464         // update queue DB
465         if (dp_db_replace_column
466                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_PROGRESS_EVENT,
467                         DP_DB_COL_TYPE_INT, &enable) < 0) {
468                 TRACE_ERROR("[CHECK SQL][%d]", id);
469                 return DP_ERROR_IO_ERROR;
470         }
471         // update memory
472         if (request)
473                 request->progress_cb = enable;
474         return DP_ERROR_NONE;
475 }
476
477 dp_error_type dp_request_set_network_type(int id, dp_request *request, int type)
478 {
479         if (request != NULL) {
480                 if (request->state == DP_STATE_CONNECTING ||
481                         request->state == DP_STATE_DOWNLOADING ||
482                         request->state == DP_STATE_COMPLETED) {
483                         TRACE_ERROR
484                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
485                         return DP_ERROR_INVALID_STATE;
486                 }
487         } else {
488                 // check id in logging table.
489                 dp_state_type state =
490                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
491
492                 if (state <= DP_STATE_NONE) {
493                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
494                         return DP_ERROR_ID_NOT_FOUND;
495                 }
496                 // check again from logging table
497                 if (state == DP_STATE_CONNECTING ||
498                         state == DP_STATE_DOWNLOADING ||
499                         state == DP_STATE_COMPLETED) {
500                         TRACE_ERROR("[ERROR][%d] now[%s]", id, dp_print_state(state));
501                         return DP_ERROR_INVALID_STATE;
502                 }
503         }
504
505         // update queue DB
506         if (dp_db_replace_column
507                         (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_NETWORK_TYPE,
508                         DP_DB_COL_TYPE_INT, &type) < 0) {
509                 TRACE_ERROR("[CHECK SQL][%d]", id);
510                 return DP_ERROR_IO_ERROR;
511         }
512         // update memory
513         if (request)
514                 request->network_type = type;
515         return DP_ERROR_NONE;
516 }
517
518 char *dp_request_get_url(int id, dp_request *request, dp_error_type *errorcode)
519 {
520         char *url = NULL;
521
522         if (request == NULL) {
523                 dp_state_type state =
524                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
525
526                 if (state <= DP_STATE_NONE) {
527                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
528                         *errorcode = DP_ERROR_ID_NOT_FOUND;
529                         return NULL;
530                 }
531         }
532         url = dp_db_get_text_column
533                                 (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_URL);
534         if (url == NULL) {
535                 *errorcode = DP_ERROR_NO_DATA;
536                 return NULL;
537         }
538         return url;
539 }
540
541 char *dp_request_get_destination(int id, dp_request *request, dp_error_type *errorcode)
542 {
543         char *dest = NULL;
544
545         if (request == NULL) {
546                 dp_state_type state =
547                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
548
549                 if (state <= DP_STATE_NONE) {
550                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
551                         *errorcode = DP_ERROR_ID_NOT_FOUND;
552                         return NULL;
553                 }
554         }
555         dest = dp_db_get_text_column
556                                 (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_DESTINATION);
557         if (dest == NULL) {
558                 *errorcode = DP_ERROR_NO_DATA;
559                 return NULL;
560         }
561         return dest;
562 }
563
564 char *dp_request_get_filename(int id, dp_request *request, dp_error_type *errorcode)
565 {
566         char *filename = NULL;
567
568         if (request == NULL) {
569                 dp_state_type state =
570                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
571
572                 if (state <= DP_STATE_NONE) {
573                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
574                         *errorcode = DP_ERROR_ID_NOT_FOUND;
575                         return NULL;
576                 }
577         }
578         filename = dp_db_get_text_column
579                                 (id, DP_DB_TABLE_REQUEST_INFO, DP_DB_COL_FILENAME);
580         if (filename == NULL) {
581                 *errorcode = DP_ERROR_NO_DATA;
582                 return NULL;
583         }
584         return filename;
585 }
586
587 char *dp_request_get_contentname(int id, dp_request *request, dp_error_type *errorcode)
588 {
589         char *content = NULL;
590
591         if (request == NULL) {
592                 dp_state_type state =
593                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
594
595                 if (state <= DP_STATE_NONE) {
596                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
597                         *errorcode = DP_ERROR_ID_NOT_FOUND;
598                         return NULL;
599                 }
600         }
601         content = dp_db_get_text_column
602                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_CONTENT_NAME);
603         if (content == NULL) {
604                 *errorcode = DP_ERROR_NO_DATA;
605                 return NULL;
606         }
607         return content;
608 }
609
610 char *dp_request_get_etag(int id, dp_request *request, dp_error_type *errorcode)
611 {
612         char *etag = NULL;
613
614         if (request == NULL) {
615                 dp_state_type state =
616                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
617
618                 if (state <= DP_STATE_NONE) {
619                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
620                         *errorcode = DP_ERROR_ID_NOT_FOUND;
621                         return NULL;
622                 }
623         }
624         etag = dp_db_get_text_column
625                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_ETAG);
626         if (etag == NULL) {
627                 *errorcode = DP_ERROR_NO_DATA;
628                 return NULL;
629         }
630         return etag;
631 }
632
633 char *dp_request_get_savedpath(int id, dp_request *request, dp_error_type *errorcode)
634 {
635         char *savedpath = NULL;
636
637         if (request == NULL) {
638                 dp_state_type state =
639                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
640
641                 if (state <= DP_STATE_NONE) {
642                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
643                         *errorcode = DP_ERROR_ID_NOT_FOUND;
644                         return NULL;
645                 }
646         }
647         savedpath = dp_db_get_text_column
648                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_SAVED_PATH);
649         if (savedpath == NULL) {
650                 *errorcode = DP_ERROR_NO_DATA;
651                 return NULL;
652         }
653         return savedpath;
654 }
655
656 char *dp_request_get_tmpsavedpath(int id, dp_request *request, dp_error_type *errorcode)
657 {
658         char *tmppath = NULL;
659
660         if (request == NULL) {
661                 dp_state_type state =
662                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
663
664                 if (state <= DP_STATE_NONE) {
665                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
666                         *errorcode = DP_ERROR_ID_NOT_FOUND;
667                         return NULL;
668                 }
669         }
670         tmppath = dp_db_get_text_column
671                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_TMP_SAVED_PATH);
672         if (tmppath == NULL) {
673                 *errorcode = DP_ERROR_NO_DATA;
674                 return NULL;
675         }
676         return tmppath;
677 }
678
679 char *dp_request_get_mimetype(int id, dp_request *request, dp_error_type *errorcode)
680 {
681         char *mimetype = NULL;
682
683         if (request == NULL) {
684                 dp_state_type state =
685                         dp_db_get_int_column(id, DP_DB_TABLE_LOG, DP_DB_COL_STATE);
686
687                 if (state <= DP_STATE_NONE) {
688                         TRACE_ERROR("[ERROR][%d] state[%s]", id, dp_print_state(state));
689                         *errorcode = DP_ERROR_ID_NOT_FOUND;
690                         return NULL;
691                 }
692         }
693         mimetype = dp_db_get_text_column
694                                 (id, DP_DB_TABLE_DOWNLOAD_INFO, DP_DB_COL_MIMETYPE);
695         if (mimetype == NULL) {
696                 *errorcode = DP_ERROR_NO_DATA;
697                 return NULL;
698         }
699         return mimetype;
700 }
701
702 dp_request *dp_request_load_from_log(int id, dp_error_type *errorcode)
703 {
704         dp_request *request = NULL;
705
706         request = dp_db_load_logging_request(id);
707         if (request == NULL) {
708                 *errorcode = DP_ERROR_ID_NOT_FOUND;
709                 return NULL;
710         }
711         if (request->state == DP_STATE_COMPLETED) {
712                 TRACE_ERROR
713                         ("[ERROR][%d] now[%s]", id, dp_print_state(request->state));
714                 *errorcode =  DP_ERROR_INVALID_STATE;
715                 dp_request_free(request);
716                 return NULL;
717         }
718         return request;
719 }
720