1 | #ifndef DOWNLOAD_H |
---|
2 | #define DOWNLOAD_H |
---|
3 | |
---|
4 | int screendownload(char* title, char* host, char* page, int port, char* filename, char* auth, int timeout, int flag) |
---|
5 | { |
---|
6 | int rcret = -1, count = 0, ret = 0, fromthread = 0, sleeptime = 2; |
---|
7 | struct skin* download = getscreen("download"); |
---|
8 | struct skin* progress = getscreennode(download, "progress"); |
---|
9 | struct skin* file = getscreennode(download, "file"); |
---|
10 | struct skin* maxkb = getscreennode(download, "maxkb"); |
---|
11 | struct skin* aktkb = getscreennode(download, "aktkb"); |
---|
12 | struct skin* framebuffer = getscreen("framebuffer"); |
---|
13 | char* bg = NULL, *tmpstr = NULL; |
---|
14 | struct download* dnode = NULL; |
---|
15 | |
---|
16 | if(pthread_self() != status.mainthread) |
---|
17 | fromthread = 1; |
---|
18 | |
---|
19 | if(filename == NULL) |
---|
20 | { |
---|
21 | err("filename = NULL"); |
---|
22 | return 1; |
---|
23 | } |
---|
24 | |
---|
25 | changetitle(download, title); |
---|
26 | changetext(file, filename); |
---|
27 | progress->progresssize = 0; |
---|
28 | changetext(maxkb, NULL); |
---|
29 | changetext(aktkb, NULL); |
---|
30 | |
---|
31 | if(fromthread == 1) |
---|
32 | { |
---|
33 | delrc(getrcconfigint("rcvolup", NULL), NULL, NULL); |
---|
34 | delrc(getrcconfigint("rcvoldown", NULL), NULL, NULL); |
---|
35 | delrc(getrcconfigint("rcmute", NULL), NULL, NULL); |
---|
36 | m_lock(&status.drawingmutex, 0); |
---|
37 | m_lock(&status.rcmutex, 10); |
---|
38 | setnodeattr(download, framebuffer, 2); |
---|
39 | status.rcowner = download; |
---|
40 | bg = savescreen(download); |
---|
41 | drawscreen(download, 0, 2); |
---|
42 | } |
---|
43 | else |
---|
44 | drawscreen(download, 0, 0); |
---|
45 | |
---|
46 | |
---|
47 | dnode = calloc(1, sizeof(struct download)); |
---|
48 | if(dnode == NULL) |
---|
49 | { |
---|
50 | err("no mem"); |
---|
51 | return 1; |
---|
52 | } |
---|
53 | |
---|
54 | dnode->host = host; |
---|
55 | dnode->page = page; |
---|
56 | dnode->port = port; |
---|
57 | dnode->filename = filename; |
---|
58 | dnode->auth = auth; |
---|
59 | dnode->connfd = -1; |
---|
60 | dnode->ret = -1; |
---|
61 | dnode->timeout = timeout; |
---|
62 | |
---|
63 | addtimer(&gethttpstruct, START, 1000, 1, (void*)dnode, NULL, NULL); |
---|
64 | |
---|
65 | while(1) |
---|
66 | { |
---|
67 | rcret = waitrc(download, 1000, 0); |
---|
68 | |
---|
69 | if(rcret == RCTIMEOUT) |
---|
70 | { |
---|
71 | progress->progresssize = dnode->proz; |
---|
72 | tmpstr = oitoa(dnode->maxkb / 1024); |
---|
73 | tmpstr = ostrcat(_("Filesize (KB): "), tmpstr, 0, 1); |
---|
74 | changetext(maxkb, tmpstr); |
---|
75 | free(tmpstr); tmpstr = NULL; |
---|
76 | tmpstr = oitoa(dnode->aktkb / 1024); |
---|
77 | tmpstr = ostrcat(_("Download (KB): "), tmpstr, 0, 1); |
---|
78 | changetext(aktkb, tmpstr); |
---|
79 | free(tmpstr); tmpstr = NULL; |
---|
80 | } |
---|
81 | drawscreen(download, 0, 0); |
---|
82 | if(rcret == getrcconfigint("rcexit", NULL)) break; |
---|
83 | if(dnode->ret == 0) |
---|
84 | { |
---|
85 | progress->progresssize = dnode->proz; |
---|
86 | tmpstr = oitoa(dnode->aktkb / 1024); |
---|
87 | tmpstr = ostrcat(_("Download (KB): "), tmpstr, 0, 1); |
---|
88 | changetext(aktkb, tmpstr); |
---|
89 | free(tmpstr); tmpstr = NULL; |
---|
90 | break; |
---|
91 | } |
---|
92 | if(dnode->ret > 0) break; |
---|
93 | } |
---|
94 | |
---|
95 | if(dnode->ret > 0 && rcret != getrcconfigint("rcexit", NULL)) |
---|
96 | { |
---|
97 | changetext(file, _("download error")); |
---|
98 | sleeptime = 5; |
---|
99 | } |
---|
100 | else |
---|
101 | changetext(file, _("wait for stopping download")); |
---|
102 | drawscreen(download, 0, 0); |
---|
103 | sockclose(&dnode->connfd); |
---|
104 | sleep(sleeptime); |
---|
105 | count = 0; |
---|
106 | while(dnode->ret < 0 && count < 10) |
---|
107 | { |
---|
108 | count++; |
---|
109 | sleep(1); |
---|
110 | } |
---|
111 | ret = dnode->ret; |
---|
112 | |
---|
113 | if(count < 10) |
---|
114 | { |
---|
115 | free(dnode); |
---|
116 | dnode = NULL; |
---|
117 | } |
---|
118 | else |
---|
119 | addoldentry((void*)dnode, 1, time(NULL) + 7200, NULL); |
---|
120 | |
---|
121 | if(fromthread == 1) |
---|
122 | { |
---|
123 | clearscreennolock(download); |
---|
124 | restorescreen(bg, download); |
---|
125 | blitfb(0); |
---|
126 | sleep(1); |
---|
127 | status.rcowner = NULL; |
---|
128 | m_unlock(&status.rcmutex, 10); |
---|
129 | m_unlock(&status.drawingmutex, 0); |
---|
130 | addrc(getrcconfigint("rcvolup", NULL), screenvolumeup, NULL, NULL); |
---|
131 | addrc(getrcconfigint("rcvoldown", NULL), screenvolumedown, NULL, NULL); |
---|
132 | addrc(getrcconfigint("rcmute", NULL), screenmute, NULL, NULL); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | clearscreen(download); |
---|
137 | drawscreen(skin, 0, 0); |
---|
138 | } |
---|
139 | |
---|
140 | return ret; |
---|
141 | } |
---|
142 | |
---|
143 | #endif |
---|