2
* Copyright (C) 2008 Michael Lamothe
4
* This file is part of Me TV
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.
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.
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
21
#include "scan_dialog.h"
22
#include "application.h"
23
#include "exception_handler.h"
28
#define COLUMN_REGION 0
29
#define BUFFER_SIZE 100
31
#ifndef SCAN_DIRECTORY
32
#define SCAN_DIRECTORY "/usr/share/doc/dvb-utils/examples/scan"
40
Timeout(guint interval, GSourceFunc function, gpointer data)
42
timeout_id = gdk_threads_add_timeout (interval, function, data);
47
g_source_remove(timeout_id);
51
ScanDialog::ScanDialog(int frontend_type)
53
Application& application = Application::get_current();
54
Glade& glade = application.get_glade();
56
scan_complete = false;
59
this->frontend_type = frontend_type;
61
g_static_rec_mutex_init (&scan_mutex);
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");
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));
72
selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(tree_view_select_region));
73
gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
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);
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);
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 );
85
String ScanDialog::get_initial_tuning_dir()
87
String path = SCAN_DIRECTORY;
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;
96
throw Exception("Unknown frontend type");
102
void ScanDialog::show()
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);
111
throw Exception(_("Failed to get directory: '%s'"), error->message);
114
const gchar* filename = g_dir_read_name(directory);
115
while (filename != NULL)
118
gtk_tree_store_append(GTK_TREE_STORE(model), &iter, NULL);
119
gtk_tree_store_set(GTK_TREE_STORE(model), &iter, 0, filename, -1);
121
filename = g_dir_read_name(directory);
123
g_dir_close(directory);
125
Timeout timeout(500, process_scan, this);
127
gboolean done = false;
130
int response = gtk_dialog_run(GTK_DIALOG(scan_dialog));
134
Application::get_current().show_error_message_dialog("Me TV scan cancelled");
136
else if (response == -1)
142
gtk_widget_hide(scan_dialog);
145
void ScanDialog::on_region_selected()
147
gtk_notebook_next_page(GTK_NOTEBOOK(notebook_scan));
148
gtk_widget_hide(button_scan_dialog_next);
150
MutexLock mutex(&scan_mutex);
151
GError* error = NULL;
152
scan_thread = g_thread_create(scan_thread_func, this, TRUE, &error);
155
throw Exception("Failed to create standard_output read thead: %s", error->message);
159
gpointer ScanDialog::scan_thread_func(gpointer data)
161
ScanDialog* scan_dialog = (ScanDialog*)data;
167
void ScanDialog::append_text(const gchar* text)
169
MutexLock mutex(&scan_mutex);
173
void ScanDialog::scan()
177
String path = get_initial_tuning_dir();
181
int result = scan_main(path.c_str(), this);
184
throw Exception("Scan failed: %d", result);
187
scan_complete = true;
192
gboolean ScanDialog::process_scan(gpointer data)
194
gboolean result = TRUE;
198
ScanDialog* scan_dialog = (ScanDialog*)data;
200
MutexLock mutex(&scan_dialog->scan_mutex);
202
gsize length = scan_dialog->text_buffer.get_buffer_length();
205
GtkTextBuffer* buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(scan_dialog->text_view_scan_progress));
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 = "";
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);
217
if (scan_dialog->scan_complete)
219
gtk_dialog_response(GTK_DIALOG(scan_dialog->scan_dialog), -1);
228
void ScanDialog::on_tree_view_select_region_row_activated(
229
GtkTreeView* tree_view,
231
GtkTreeViewColumn *column,
232
ScanDialog* scan_dialog)
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);
240
gtk_tree_model_get(model, &iter, COLUMN_REGION, &r, -1);
241
scan_dialog->region = r;
243
scan_dialog->on_region_selected();
247
void ScanDialog::button_scan_dialog_next_clicked(GtkButton* button, ScanDialog* scan_dialog)
252
GtkTreeModel* model = NULL;
253
if (!gtk_tree_selection_get_selected(scan_dialog->selection, &model, &iter))
255
throw Exception("Please select a region first");
260
gtk_tree_model_get(model, &iter, COLUMN_REGION, &r, -1);
261
scan_dialog->region = r;
263
scan_dialog->on_region_selected();