1 #include <string.h>
2 #include <stdlib.h>
3 #include "utility.h"
4 #include "urlgfe.h"
5 #include "download_setting.h"
6
7
download_setting_init(DownloadSetting * dd)8 DownloadSetting* download_setting_init (DownloadSetting* dd)
9 {
10 proxy_setting_init(&dd->proxy);
11
12 dd->n_parts = 0;
13 dd->n_redirect = 0;
14 dd->n_retries = 99;
15 dd->retry_delay = 3;
16 dd->multiple = 0; // FALSE
17 dd->start_mode = DOWNLOAD_START_AUTO;
18 dd->state = DOWNLOAD_STATE_WAITING;
19 dd->url = NULL;
20 dd->filename = NULL;
21 dd->directory = NULL;
22 dd->referer = NULL;
23
24 return dd;
25 }
26
download_setting_clear(DownloadSetting * dd)27 void download_setting_clear (DownloadSetting* dd)
28 {
29 g_free (dd->url);
30 g_free (dd->filename);
31 g_free (dd->directory);
32 g_free (dd->referer);
33 dd->url = NULL;
34 dd->filename = NULL;
35 dd->directory = NULL;
36 dd->referer = NULL;
37
38 proxy_setting_clear (&dd->proxy);
39 }
40
download_setting_destroy(DownloadSetting * dd)41 void download_setting_destroy (DownloadSetting* dd)
42 {
43 download_setting_clear (dd);
44 g_free (dd);
45 }
46
download_setting_assign(DownloadSetting * dd,DownloadSetting * src)47 void download_setting_assign (DownloadSetting* dd, DownloadSetting* src)
48 {
49 proxy_setting_assign (&dd->proxy, &src->proxy);
50
51 dd->n_parts = src->n_parts;
52 dd->n_redirect = src->n_redirect;
53 dd->n_retries = src->n_retries;
54 dd->retry_delay = src->retry_delay;
55 // dd->multiple = src->multiple;
56 dd->state = src->state;
57 download_setting_set_start_mode (dd, src->start_mode);
58 download_setting_set_directory (dd, src->directory);
59 download_setting_set_referer (dd, src->referer);
60 if (dd->multiple==0 && src->multiple==0) {
61 download_setting_set_filename (dd, src->filename);
62 download_setting_set_url (dd, src->url);
63 }
64 }
65
download_setting_set_start_mode(DownloadSetting * dd,int mode)66 void download_setting_set_start_mode (DownloadSetting* dd, int mode)
67 {
68 if (dd->state==DOWNLOAD_STATE_WAITING && mode==DOWNLOAD_START_MANUAL)
69 dd->state = DOWNLOAD_STATE_PAUSE;
70 else if (dd->state==DOWNLOAD_STATE_PAUSE && mode==DOWNLOAD_START_AUTO)
71 dd->state = DOWNLOAD_STATE_WAITING;
72 dd->start_mode = mode;
73 }
74
download_setting_write_xml(DownloadSetting * setting,FILE * file,int level)75 void download_setting_write_xml (DownloadSetting* setting,
76 FILE* file, int level)
77 {
78 gchar* url;
79 gchar* filename;
80 gchar* directory;
81 gchar* referer;
82 gint state = setting->state;
83
84 if (state == DOWNLOAD_STATE_EXECUTING || state == DOWNLOAD_STATE_RETRY )
85 state = DOWNLOAD_STATE_WAITING;
86
87 url = str_to_xml_str (STR_N2E (setting->url));
88 filename = str_to_xml_str (STR_N2E (setting->filename));
89 directory = str_to_xml_str (STR_N2E (setting->directory));
90 referer = str_to_xml_str (STR_N2E (setting->referer));
91
92 fprintf (file,
93 "%*c" // Start of format string
94 "<" DOWNLOAD_SETTING_TAG " "
95 "parts=\"%d\" "
96 "redirect=\"%d\" "
97 "retries=\"%d\" "
98 "retry_delay=\"%d\" "
99 "multiple=\"%d\" "
100 "start_mode=\"%d\" "
101 "state=\"%d\">\n", // End of format string
102 level, ' ',
103 setting->n_parts,
104 setting->n_redirect,
105 setting->n_retries,
106 setting->retry_delay,
107 setting->multiple,
108 setting->start_mode,
109 state
110 );
111 proxy_setting_write_xml (PROXY_SETTING (setting), file, level+1);
112
113 fprintf (file,
114 "%*c"
115 "<url>%s</url>\n"
116 "%*c"
117 "<directory>%s</directory>\n"
118 "%*c"
119 "<filename>%s</filename>\n"
120 "%*c"
121 "<referer>%s</referer>\n",
122 level+1, ' ',
123 url,
124 level+1, ' ',
125 directory,
126 level+1, ' ',
127 filename,
128 level+1, ' ',
129 referer
130 );
131
132 fprintf (file, "%*c</" DOWNLOAD_SETTING_TAG ">\n", level, ' ');
133
134 g_free (url);
135 g_free (filename);
136 g_free (directory);
137 g_free (referer);
138 }
139
140