[a2776f]: uget-cmd / UgCategory-cmd.c  Maximize  Restore  History

Download this file

280 lines (244 with data), 8.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
*
* Copyright (C) 2005-2011 by C.H. Huang
* plushuang.tw@gmail.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* ---
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
* You must obey the GNU Lesser General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
*/
#include <stdlib.h>
#include <string.h>
#include <UgData-download.h>
#include <UgCategory-cmd.h>
// ----------------------------------------------------------------------------
// UgCategoryCmd: additional data for UgCategory and command-line.
//
// UgCategory::user.category used for getting UgCategoruCmd
// UgRelation::user.category used for getting UgCategoruCmd
// UgRelation::user.storage used for getting GQueue in UgCategoryCmd
// UgRelation::user.position used for getting link in GQueue.
//
static UgCategoryCmd* ug_category_cmd_new (void);
static void ug_category_cmd_free (UgCategoryCmd* ccmd);
const UgCategoryFuncs ccmd_funcs =
{
ug_category_cmd_add,
ug_category_cmd_get_all,
ug_category_cmd_get_jobs,
ug_category_cmd_changed,
};
static UgCategoryCmd* ug_category_cmd_new (void)
{
UgCategoryCmd* ccmd;
ccmd = g_malloc (sizeof (UgCategoryCmd));
g_queue_init (&ccmd->active);
g_queue_init (&ccmd->queuing);
g_queue_init (&ccmd->finished);
g_queue_init (&ccmd->recycled);
return ccmd;
}
static void ug_category_cmd_free (UgCategoryCmd* ccmd)
{
g_queue_foreach (&ccmd->queuing, (GFunc) ug_dataset_unref, NULL);
g_queue_foreach (&ccmd->finished, (GFunc) ug_dataset_unref, NULL);
g_queue_foreach (&ccmd->recycled, (GFunc) ug_dataset_unref, NULL);
g_queue_clear (&ccmd->queuing);
g_queue_clear (&ccmd->finished);
g_queue_clear (&ccmd->recycled);
g_free (ccmd);
}
UgCategory* ug_category_new_with_cmd (void)
{
UgCategory* category;
category = ug_category_new ();
ug_category_use_cmd (category);
return category;
}
void ug_category_use_cmd (UgCategory* category)
{
if (category->user.category == NULL) {
category->user.category = ug_category_cmd_new ();
// destroy notify
category->destroy.func = (UgNotifyFunc) ug_category_cmd_free;
category->destroy.data = category->user.category;
// functions
category->funcs = &ccmd_funcs;
}
}
// add dataset to category and increase reference count of dataset.
void ug_category_cmd_add (UgCategory* category, UgDataset* dataset)
{
UgRelation* relation;
UgCategoryCmd* ccmd;
ccmd = category->user.category;
// add and set UgRelation to dataset
ug_dataset_ref (dataset);
relation = UG_DATASET_RELATION (dataset);
if (relation == NULL)
relation = ug_dataset_alloc_front (dataset, UG_RELATION_I);
relation->category = category;
relation->user.category = ccmd;
// select queue & add job to it
if (relation->hints & UG_HINT_RECYCLED) {
g_queue_push_head (&ccmd->recycled, dataset);
relation->user.storage = &ccmd->recycled;
relation->user.position = ccmd->recycled.head;
}
else if (relation->hints & UG_HINT_FINISHED) {
g_queue_push_head (&ccmd->finished, dataset);
relation->user.storage = &ccmd->finished;
relation->user.position = ccmd->finished.head;
}
else {
g_queue_push_tail (&ccmd->queuing, dataset);
relation->user.storage = &ccmd->queuing;
relation->user.position = ccmd->queuing.tail;
}
}
// get all jobs(UgDataset) in this category.
// To free the returned value, use g_list_free (list).
GList* ug_category_cmd_get_all (UgCategory* category)
{
UgCategoryCmd* ccmd;
GList* link;
GList* list;
ccmd = category->user.category;
list = NULL;
for (link = ccmd->recycled.tail; link; link = link->prev)
list = g_list_prepend (list, link->data);
for (link = ccmd->finished.tail; link; link = link->prev)
list = g_list_prepend (list, link->data);
for (link = ccmd->queuing.tail; link; link = link->prev)
list = g_list_prepend (list, link->data);
return list;
}
// get queuing jobs(UgDataset) in this category/
// This function should be noticed UgCategory::active_limit, because
// application will try to activate all returned dataset.
// To free the returned value, use g_list_free (list).
GList* ug_category_cmd_get_jobs (UgCategory* category)
{
UgCategoryCmd* ccmd;
UgRelation* relation;
GList* link;
GList* jobs;
guint jobs_len;
ccmd = category->user.category;
if (category->active_limit <= ccmd->active.length)
return NULL;
jobs = NULL;
jobs_len = 0;
for (link = ccmd->queuing.head; link; link = link->next) {
relation = UG_DATASET_RELATION ((UgDataset*) link->data);
if (relation->hints & UG_HINT_UNRUNNABLE)
continue;
jobs = g_list_prepend (jobs, link->data);
jobs_len++;
if (jobs_len >= (category->active_limit - ccmd->active.length))
break;
}
return g_list_reverse (jobs);
}
// used to notify category that it's dataset was changed.
// It may change hints and switch dataset to another internal queue of category.
void ug_category_cmd_changed (UgCategory* category, UgDataset* dataset)
{
UgCategoryCmd* ccmd;
UgRelation* relation;
GQueue* queue;
ccmd = category->user.category;
relation = UG_DATASET_RELATION (dataset);
// select queue
if (relation->hints & UG_HINT_RECYCLED) {
queue = &ccmd->recycled;
relation->hints &= ~UG_HINT_FINISHED;
}
else if (relation->hints & UG_HINT_FINISHED) {
queue = &ccmd->finished;
relation->hints &= ~UG_HINT_RECYCLED;
}
else if (relation->hints & UG_HINT_ACTIVE)
queue = &ccmd->active;
else
queue = &ccmd->queuing;
// Don't switch job to the same queue.
if (queue != relation->user.storage) {
// switch job to new queue
g_queue_delete_link (relation->user.storage, relation->user.position);
if (queue == &ccmd->finished || queue == &ccmd->recycled) {
g_queue_push_head (queue, dataset);
relation->user.storage = queue;
relation->user.position = queue->head;
}
else {
g_queue_push_tail (queue, dataset);
relation->user.storage = queue;
relation->user.position = queue->tail;
}
}
}
void ug_category_cmd_remove (UgCategory* category, UgDataset* dataset)
{
UgRelation* relation;
relation = UG_DATASET_RELATION (dataset);
relation->category = NULL;
relation->user.category = NULL;
g_queue_delete_link (relation->user.storage, relation->user.position);
// delete data and files
ug_download_delete_temp (dataset);
ug_dataset_unref (dataset);
}
void ug_category_cmd_clear (UgCategory* category, UgCategoryHints hint, guint from_nth)
{
UgCategoryCmd* ccmd;
GQueue* queue;
ccmd = category->user.category;
// select queue
if (hint & UG_HINT_RECYCLED)
queue = &ccmd->recycled;
else if (hint & UG_HINT_FINISHED)
queue = &ccmd->finished;
else
queue = &ccmd->queuing;
while (queue->length > from_nth)
ug_category_cmd_remove (category, queue->tail->data);
}
void ug_category_cmd_move_to (UgCategory* category, UgDataset* dataset, UgCategory* dest)
{
UgRelation* relation;
relation = UG_DATASET_RELATION (dataset);
if (relation->category == category)
return;
// move
ug_dataset_ref (dataset);
ug_category_cmd_remove (category, dataset);
ug_category_cmd_add (category, dataset);
ug_dataset_unref (dataset);
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks