1 #include <stdlib.h>
2 #include "download_store.h"
3 #include "download_thread.h"
4 
download_store_init(DownloadStore * ds)5 void download_store_init (DownloadStore* ds)
6 {
7 	ds->list_store = gtk_list_store_new (DOWNLOAD_N_COL,
8 	                                     G_TYPE_POINTER,
9 	                                     G_TYPE_POINTER);
10 
11 	ds->capacity_limit = 0;
12 	ds->historical = FALSE;
13 	g_static_rec_mutex_init (&ds->mutex);
14 }
15 
download_store_clear(DownloadStore * ds)16 void download_store_clear (DownloadStore* ds)
17 {
18 	// remove data
19 	download_store_erase_from_nth (ds, 0);
20 	g_object_unref (ds->list_store);
21 
22 	g_static_rec_mutex_free (&ds->mutex);
23 }
24 
download_store_add_download_allocated(DownloadStore * ds,Download * dl,gboolean prepend)25 void download_store_add_download_allocated (DownloadStore* ds,
26                                          Download*      dl,
27                                          gboolean       prepend)
28 {
29 	GtkTreeIter iter;
30 
31 	download_store_lock (ds);
32 
33 	if (prepend)
34 		gtk_list_store_prepend (ds->list_store, &iter);
35 	else
36 		gtk_list_store_append (ds->list_store, &iter);
37 
38 	gtk_list_store_set (ds->list_store, &iter,
39 	                    DOWNLOAD_COL_DATA, dl,
40 	                    -1);
41 
42 	download_store_unlock (ds);
43 
44 	download_store_erase_excess (ds);
45 }
46 
download_store_add(DownloadStore * ds,Download * dl)47 void download_store_add (DownloadStore* ds, Download* dl)
48 {
49 	Download* temp;
50 
51 	temp = download_new ();
52 	download_assign (temp, dl);
53 
54 	download_store_add_allocated (ds, temp);
55 }
56 
download_store_erase(DownloadStore * ds,GtkTreeIter * iter)57 void download_store_erase (DownloadStore* ds, GtkTreeIter* iter)
58 {
59 	Download* dl_data;
60 	gpointer  thread;
61 
62 	download_store_lock (ds);
63 
64 	gtk_tree_model_get ( (GtkTreeModel*)ds->list_store, iter,
65 	                    DOWNLOAD_COL_DATA, &dl_data,
66 	                    DOWNLOAD_COL_THREAD, &thread,
67 	                    -1);
68 	if (thread)
69 		download_thread_destroy (thread);
70 
71 	download_unref (dl_data);
72 	gtk_list_store_remove (ds->list_store, iter);
73 
74 	download_store_unlock (ds);
75 }
76 
download_store_erase_excess(DownloadStore * ds)77 void download_store_erase_excess (DownloadStore* ds)
78 {
79 	guint n;
80 
81 	n = gtk_tree_model_iter_n_children (GTK_TREE_MODEL (ds->list_store), NULL);
82 
83 	if (ds->historical && n > ds->capacity_limit)
84 		download_store_erase_from_nth (ds, ds->capacity_limit);
85 }
86 
download_store_erase_from_nth(DownloadStore * ds,guint nth)87 void download_store_erase_from_nth (DownloadStore* ds, guint nth)
88 {
89 	GtkTreeIter iter, iter_next;
90 	gboolean    valid;
91 
92 	if (ds->historical==FALSE)
93 		return;
94 
95 	valid = gtk_tree_model_iter_nth_child (GTK_TREE_MODEL (ds->list_store),
96 	                                       &iter, NULL, nth);
97     while (valid) {
98     	iter_next = iter;
99     	valid = gtk_tree_model_iter_next (GTK_TREE_MODEL (ds->list_store),
100     	                                  &iter_next);
101     	download_store_erase (ds, &iter);
102     }
103 }
104 
download_store_move_to_store(DownloadStore * ds_src,DownloadStore * ds_dest,GtkTreeIter * iter_src)105 void download_store_move_to_store (DownloadStore* ds_src,
106                                    DownloadStore* ds_dest,
107                                    GtkTreeIter* iter_src)
108 {
109 	Download*        dl_data;
110 	DownloadThread*  dt;
111 
112 	download_store_lock (ds_src);
113 
114 	gtk_tree_model_get ((GtkTreeModel*)ds_src->list_store,
115 	                    iter_src,
116 	                    DOWNLOAD_COL_DATA, &dl_data,
117 	                    DOWNLOAD_COL_THREAD, &dt,
118 	                    -1);
119     if (dt)
120         download_thread_job_erased (dt);
121 
122 	download_store_lock (ds_dest);
123 
124 	download_store_add (ds_dest, dl_data);
125 	gtk_list_store_remove (ds_src->list_store, iter_src);
126 
127 	download_store_unlock (ds_dest);
128 
129 	download_store_unlock (ds_src);
130 }
131 
download_store_find_waiting(DownloadStore * ds,GtkTreeIter * iter)132 gboolean download_store_find_waiting (DownloadStore* ds,
133                                       GtkTreeIter*   iter)
134 {
135 	Download* dl_data;
136 	gboolean  return_val = FALSE;
137 	gboolean  valid;
138 	gpointer  thread;
139 
140 //	download_store_lock (ds);
141 
142 	valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(ds->list_store),
143 	                                       iter);
144 
145 	while (valid) {
146 		gtk_tree_model_get ( (GtkTreeModel*)ds->list_store, iter,
147 		                    DOWNLOAD_COL_DATA,   &dl_data,
148 		                    DOWNLOAD_COL_THREAD, &thread,
149 		                    -1);
150 		if (DOWNLOAD_SETTING (dl_data)->state == DOWNLOAD_STATE_WAITING &&
151 		    thread==NULL )
152 		{
153 			return_val = TRUE;
154 			break;
155 		}
156 		valid = gtk_tree_model_iter_next ( (GtkTreeModel*)ds->list_store,
157 		                                   iter);
158 	}
159 
160 //	download_store_unlock (ds);
161 
162 	return return_val;
163 }
164 
download_store_row_changed(DownloadStore * ds,GtkTreeIter * iter)165 void download_store_row_changed (DownloadStore* ds,
166                                  GtkTreeIter* iter)
167 {
168 	GtkTreePath* path;
169 
170 	path = gtk_tree_model_get_path ((GtkTreeModel*)ds->list_store,
171 	                                iter);
172 	gtk_tree_model_row_changed ((GtkTreeModel*)ds->list_store,
173 	                            path, iter);
174 	gtk_tree_path_free (path);
175 }
176 
download_store_set_thread(DownloadStore * ds,GtkTreeIter * iter,gpointer ptr)177 void download_store_set_thread(DownloadStore*  ds,
178                                GtkTreeIter*    iter,
179                                gpointer        ptr)
180 {
181 	DownloadThread*    thread = ptr;
182 
183 	gtk_list_store_set (ds->list_store,
184 	                    iter,
185 	                    DOWNLOAD_COL_THREAD,
186 	                    thread,
187 	                    -1);
188 }
189 
190