summaryrefslogtreecommitdiff
path: root/src/importwindow.c
blob: cf269c8150f4b57bd4a5a4214d625e8ffc3e4948 (plain)
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
/***************************************************************************
 *            importwindow.c
 *
 *  Copyright  2005  Ian McIntosh
 *  ian_mcintosh@linuxadvocate.org
 ****************************************************************************/

/*
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <glade/glade.h>
#include <gtk/gtk.h>
#include "main.h"
#include "db.h"
#include "import.h"
#include "mainwindow.h"
#include "importwindow.h"
#include "util.h"
#include "gui.h"

struct {
	GtkWindow* pWindow;
	GtkProgressBar* pProgressBar;
	GtkButton* pOKButton;
//	GtkButton* pCancelButton;
	GtkTextView* pLogTextView;
	
	gint nCurrentFile;
	gint nTotalFiles;
} g_ImportWindow;

void importwindow_init(GladeXML* pGladeXML)
{
	GLADE_LINK_WIDGET(pGladeXML, g_ImportWindow.pWindow, GTK_WINDOW, "importwindow");
	GLADE_LINK_WIDGET(pGladeXML, g_ImportWindow.pProgressBar, GTK_PROGRESS_BAR, "importprogressbar");
	GLADE_LINK_WIDGET(pGladeXML, g_ImportWindow.pOKButton, GTK_BUTTON, "importokbutton");
	GLADE_LINK_WIDGET(pGladeXML, g_ImportWindow.pLogTextView, GTK_TEXT_VIEW, "importlogtextview");
}

void importwindow_show(void)
{
	gtk_widget_show(GTK_WIDGET(g_ImportWindow.pWindow));
	gtk_window_present(g_ImportWindow.pWindow);
}

void importwindow_hide(void)
{
	gtk_widget_hide(GTK_WIDGET(g_ImportWindow.pWindow));
}

void importwindow_log_append(const gchar* pszFormat, ...)
{
	gchar azNewText[5000];

	va_list args;
	va_start(args, pszFormat);	
	g_vsnprintf(azNewText, 5000, pszFormat, args);
	va_end(args);

	GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(g_ImportWindow.pLogTextView);
	gtk_text_buffer_insert_at_cursor(pBuffer, azNewText, -1);

	// Scroll to end of text
	GtkTextIter iter;
	gtk_text_buffer_get_end_iter(pBuffer, &iter);
	gtk_text_view_scroll_to_iter(g_ImportWindow.pLogTextView, &iter, 0.0, FALSE,0,0);
	
	GTK_PROCESS_MAINLOOP;
}

void importwindow_progress_pulse(void)
{
//	gtk_progress_bar_set_text(g_ImportWindow.pProgressBar, azBuffer);
	
	gtk_progress_bar_pulse(g_ImportWindow.pProgressBar);

	// ensure the UI gets updated
	GTK_PROCESS_MAINLOOP;
}

void importwindow_begin(GSList* pSelectedFileList)
{
	// empty progress buffer
	GtkTextBuffer* pBuffer = gtk_text_view_get_buffer(g_ImportWindow.pLogTextView);
	gtk_text_buffer_set_text(pBuffer, "", -1);
	
	gtk_widget_show(GTK_WIDGET(g_ImportWindow.pWindow));
	gtk_window_present(g_ImportWindow.pWindow);
	gtk_widget_set_sensitive(GTK_WIDGET(g_ImportWindow.pOKButton), FALSE);

	GTK_PROCESS_MAINLOOP;

	g_ImportWindow.nTotalFiles = g_slist_length(pSelectedFileList);
	g_ImportWindow.nCurrentFile = 1;

	g_print("Importing %d file(s)\n", g_ImportWindow.nTotalFiles);

	gint nTotalSuccess = 0;
	GSList* pFile = pSelectedFileList;
	while(pFile != NULL) {
		// do some work
		if(import_from_uri((gchar*)pFile->data)) {
			nTotalSuccess++;
		}

		// Move to next file
		pFile = pFile->next;
		g_ImportWindow.nCurrentFile++;
	}
	// nTotalSuccess / g_ImportWindow.nTotalFiles

	gtk_progress_bar_set_fraction(g_ImportWindow.pProgressBar, 1.0);
	gtk_widget_set_sensitive(GTK_WIDGET(g_ImportWindow.pOKButton), TRUE);

	if(nTotalSuccess == g_ImportWindow.nTotalFiles) {
		gtk_progress_bar_set_text(g_ImportWindow.pProgressBar, "Completed Successfully");
	}
	else {
		gtk_progress_bar_set_text(g_ImportWindow.pProgressBar, "Completed with Errors");
	}
	GTK_PROCESS_MAINLOOP;

	// redraw map to show any potential new data (?)

//	map_set_zoomlevel(7);
	mainwindow_draw_map(DRAWFLAG_ALL);
}

//~ void importwindow_on_okbutton_clicked(GtkWidget* pWidget, gpointer pdata)
//~ {
	//~ // set button insensitive
	//~ gtk_widget_set_sensitive(GTK_WIDGET(g_ImportWindow.pOKButton), FALSE);

	//~ // hide dialog
	//~ gtk_widget_hide(GTK_WIDGET(g_ImportWindow.pWindow));
//~ }