~ubuntu-branches/ubuntu/precise/me-tv/precise-proposed

« back to all changes in this revision

Viewing changes to src/scan_dialog.cc

  • Committer: Bazaar Package Importer
  • Author(s): Michael Lamothe
  • Date: 2008-02-12 21:56:59 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20080212215659-g3u4v9iz8wzxu2cd
Tags: 0.5.17-1
New upstream release (Closes: #464922)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2008 Michael Lamothe
 
3
 *
 
4
 * This file is part of Me TV
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 * 
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU Library General Public License for more details.
 
15
 * 
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301,  USA
 
19
 */
 
20
 
 
21
#include "scan_dialog.h"
 
22
#include "application.h"
 
23
#include "exception_handler.h"
 
24
#include "config.h"
 
25
#include "scan.h"
 
26
#include <sys/wait.h>
 
27
 
 
28
#define COLUMN_REGION   0
 
29
#define BUFFER_SIZE             100
 
30
 
 
31
#ifndef SCAN_DIRECTORY
 
32
#define SCAN_DIRECTORY "/usr/share/doc/dvb-utils/examples/scan"
 
33
#endif
 
34
 
 
35
class Timeout
 
36
{
 
37
private:
 
38
        guint timeout_id;
 
39
public:
 
40
        Timeout(guint interval, GSourceFunc function, gpointer data)
 
41
        {
 
42
                timeout_id = gdk_threads_add_timeout (interval, function, data);        
 
43
        }
 
44
        
 
45
        ~Timeout()
 
46
        {
 
47
                g_source_remove(timeout_id);
 
48
        }
 
49
};
 
50
 
 
51
ScanDialog::ScanDialog(int frontend_type)
 
52
{
 
53
        Application& application = Application::get_current();
 
54
        Glade& glade = application.get_glade();
 
55
        scan_thread = NULL;
 
56
        scan_complete = false;
 
57
        pid = 0;
 
58
 
 
59
        this->frontend_type = frontend_type;
 
60
        
 
61
        g_static_rec_mutex_init (&scan_mutex);
 
62
 
 
63
        scan_dialog                             = glade.get_widget("scan_dialog");
 
64
        tree_view_select_region = glade.get_widget("tree_view_select_region");
 
65
        notebook_scan                   = glade.get_widget("notebook_scan");
 
66
        button_scan_dialog_next = glade.get_widget("button_scan_dialog_next");
 
67
        text_view_scan_progress = glade.get_widget("text_view_scan_progress");
 
68
        
 
69
        GtkTreeStore* tree_store = gtk_tree_store_new(1, G_TYPE_STRING);
 
70
        gtk_tree_view_set_model(GTK_TREE_VIEW(tree_view_select_region), GTK_TREE_MODEL(tree_store));
 
71
 
 
72
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_select_region));
 
73
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
 
74
        
 
75
        GtkCellRenderer*        text_renderer   = gtk_cell_renderer_text_new();
 
76
        GtkTreeViewColumn*      column_region   = gtk_tree_view_column_new_with_attributes (_("Region"), text_renderer, "text", COLUMN_REGION, NULL);
 
77
 
 
78
        gtk_tree_sortable_set_sort_column_id(GTK_TREE_SORTABLE(tree_store), COLUMN_REGION, GTK_SORT_ASCENDING);
 
79
        gtk_tree_view_append_column(GTK_TREE_VIEW(tree_view_select_region), column_region);
 
80
 
 
81
        g_signal_connect ( G_OBJECT ( tree_view_select_region ), "row-activated", G_CALLBACK ( on_tree_view_select_region_row_activated ), this );
 
82
        g_signal_connect( G_OBJECT ( button_scan_dialog_next ), "clicked", G_CALLBACK ( button_scan_dialog_next_clicked ), this );
 
83
}
 
84
 
 
85
String ScanDialog::get_initial_tuning_dir()
 
86
{
 
87
        String path = SCAN_DIRECTORY;
 
88
        
 
89
        switch(frontend_type)
 
90
        {
 
91
                case FE_OFDM:   path += "/dvb-t";       break;
 
92
                case FE_QAM:    path += "/dvb-c";       break;
 
93
                case FE_QPSK:   path += "/dvb-s";       break;
 
94
                case FE_ATSC:   path += "/atsc";        break;
 
95
                default:
 
96
                        throw Exception("Unknown frontend type");
 
97
        }
 
98
        
 
99
        return path;
 
100
}
 
101
 
 
102
void ScanDialog::show()
 
103
{
 
104
        GtkTreeModel* model = gtk_tree_view_get_model(GTK_TREE_VIEW(tree_view_select_region));
 
105
        String path = get_initial_tuning_dir();
 
106
        GError* error = NULL;
 
107
        GDir* directory = g_dir_open(path.c_str(), 0, &error);
 
108
        
 
109
        if (error != NULL)
 
110
        {
 
111
                throw Exception(_("Failed to get directory: '%s'"), error->message);
 
112
        }
 
113
 
 
114
        const gchar* filename = g_dir_read_name(directory);
 
115
        while (filename != NULL)
 
116
        {
 
117
                GtkTreeIter iter;
 
118
                gtk_tree_store_append(GTK_TREE_STORE(model), &iter, NULL);
 
119
                gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, filename, -1);
 
120
                
 
121
                filename = g_dir_read_name(directory);
 
122
        }
 
123
        g_dir_close(directory);
 
124
        
 
125
        Timeout timeout(500, process_scan, this);
 
126
 
 
127
        gboolean done = false;
 
128
        while (!done)
 
129
        {
 
130
                int response = gtk_dialog_run(GTK_DIALOG(scan_dialog));
 
131
                if (response == 0)
 
132
                {
 
133
                        done = true;
 
134
                        Application::get_current().show_error_message_dialog("Me TV scan cancelled");
 
135
                }
 
136
                else if (response == -1)
 
137
                {
 
138
                        done = true;
 
139
                }
 
140
        }
 
141
        
 
142
        gtk_widget_hide(scan_dialog);
 
143
}
 
144
 
 
145
void ScanDialog::on_region_selected()
 
146
{
 
147
        gtk_notebook_next_page(GTK_NOTEBOOK(notebook_scan));
 
148
        gtk_widget_hide(button_scan_dialog_next);
 
149
        
 
150
        MutexLock mutex(&scan_mutex);
 
151
        GError* error = NULL;
 
152
        scan_thread = g_thread_create(scan_thread_func, this, TRUE, &error);
 
153
        if (error != NULL)
 
154
        {
 
155
                throw Exception("Failed to create standard_output read thead: %s", error->message);
 
156
        }
 
157
}
 
158
 
 
159
gpointer ScanDialog::scan_thread_func(gpointer data)
 
160
{
 
161
        ScanDialog* scan_dialog = (ScanDialog*)data;
 
162
        scan_dialog->scan();
 
163
        
 
164
        return NULL;
 
165
}
 
166
 
 
167
void ScanDialog::append_text(const gchar* text)
 
168
{
 
169
        MutexLock mutex(&scan_mutex);
 
170
        text_buffer += text;
 
171
}
 
172
 
 
173
void ScanDialog::scan()
 
174
{
 
175
        TRY;
 
176
        
 
177
        String path = get_initial_tuning_dir();
 
178
        path += "/";
 
179
        path += region;
 
180
        
 
181
        int result = scan_main(path.c_str(), this);
 
182
        if (result != 0)
 
183
        {
 
184
                throw Exception("Scan failed: %d", result);
 
185
        }
 
186
        
 
187
        scan_complete = true;
 
188
 
 
189
        THREAD_CATCH;
 
190
}
 
191
 
 
192
gboolean ScanDialog::process_scan(gpointer data)
 
193
{
 
194
        gboolean result = TRUE;
 
195
        
 
196
        TRY;
 
197
        
 
198
        ScanDialog* scan_dialog = (ScanDialog*)data;
 
199
                
 
200
        MutexLock mutex(&scan_dialog->scan_mutex);
 
201
                
 
202
        gsize length = scan_dialog->text_buffer.get_buffer_length();
 
203
        if (length > 0)
 
204
        {
 
205
                GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(scan_dialog->text_view_scan_progress));
 
206
                GtkTextIter end;
 
207
                gtk_text_buffer_get_end_iter(buffer, &end);
 
208
                gtk_text_buffer_insert(buffer, &end, scan_dialog->text_buffer.c_str(), length);
 
209
                scan_dialog->text_buffer = "";
 
210
 
 
211
                GtkWidget* scroll_widget = gtk_widget_get_parent(scan_dialog->text_view_scan_progress);
 
212
                GtkAdjustment* adjustment = gtk_scrolled_window_get_vadjustment(GTK_SCROLLED_WINDOW(scroll_widget));
 
213
                gtk_adjustment_set_value(adjustment, adjustment->upper - adjustment->page_size);
 
214
                gtk_scrolled_window_set_vadjustment(GTK_SCROLLED_WINDOW(scroll_widget), adjustment);
 
215
        }
 
216
        
 
217
        if (scan_dialog->scan_complete)
 
218
        {
 
219
                gtk_dialog_response(GTK_DIALOG(scan_dialog->scan_dialog), -1);
 
220
                result = FALSE;
 
221
        }
 
222
        
 
223
        CATCH;
 
224
        
 
225
        return result;
 
226
}
 
227
 
 
228
void ScanDialog::on_tree_view_select_region_row_activated(
 
229
        GtkTreeView* tree_view,
 
230
        GtkTreePath       *path,
 
231
        GtkTreeViewColumn *column,
 
232
        ScanDialog* scan_dialog)
 
233
{
 
234
        TRY;    
 
235
        GtkTreeIter iter;
 
236
        GtkTreeModel* model = gtk_tree_view_get_model(GTK_TREE_VIEW(scan_dialog->tree_view_select_region));
 
237
        gtk_tree_model_get_iter(model, &iter, path);
 
238
 
 
239
        gchar* r = NULL;
 
240
        gtk_tree_model_get(model, &iter, COLUMN_REGION, &r, -1);
 
241
        scan_dialog->region = r;
 
242
        g_free(r);
 
243
        scan_dialog->on_region_selected();
 
244
        CATCH;
 
245
}
 
246
 
 
247
void ScanDialog::button_scan_dialog_next_clicked(GtkButton* button, ScanDialog* scan_dialog)
 
248
{
 
249
        TRY;
 
250
        
 
251
        GtkTreeIter iter;
 
252
        GtkTreeModel* model = NULL;
 
253
        if (!gtk_tree_selection_get_selected(scan_dialog->selection, &model, &iter))
 
254
        {
 
255
                throw Exception("Please select a region first");
 
256
        }
 
257
        else
 
258
        {
 
259
                gchar* r = NULL;
 
260
                gtk_tree_model_get(model, &iter, COLUMN_REGION, &r, -1);
 
261
                scan_dialog->region = r;
 
262
                g_free(r);
 
263
                scan_dialog->on_region_selected();
 
264
        }
 
265
        CATCH;
 
266
}