File: guibrowser.cpp

package info (click to toggle)
libqinfinity 1.0~beta5-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 648 kB
  • ctags: 694
  • sloc: cpp: 4,943; makefile: 3
file content (320 lines) | stat: -rw-r--r-- 8,998 bytes parent folder | download
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "guibrowser.h"
#include "browser.h"
#include "createitemdialog.h"

#include <QMessageBox>
#include <QStatusBar>
#include <QDebug>
#include <QApplication>
#include <QItemSelectionModel>

#include "ui_newconnectionwidget.h"
#include "guibrowser.moc"

NewConnectionDialog::NewConnectionDialog( QWidget *parent )
	: QDialog( parent )
	, ui( new Ui_newConnectionWidget )
{
	ui->setupUi( this );
	connect( ui->okPushButton, SIGNAL(clicked(bool)),
		this, SLOT(slotOkClicked(bool)) );
	connect( ui->cancelPushButton, SIGNAL(clicked(bool)),
		this, SLOT(slotCancelClicked(bool)) );
}

void NewConnectionDialog::slotOkClicked( bool checked )
{
	if( !ui->hostnameLineEdit->text().isEmpty() &&
		!ui->portLineEdit->text().isEmpty() )
	{
		emit(createConnection( ui->hostnameLineEdit->text(),
			ui->portLineEdit->text().toUInt() ));
	}
	else
		qDebug() << "invalid input.";
	done(0);
}

void NewConnectionDialog::slotCancelClicked( bool checked )
{
	done(0);
}

BrowserMainWindow::BrowserMainWindow( QWidget *parent )
	: QMainWindow( parent )
	, fileModel( new QInfinity::BrowserModel( this ) )
	, plugin( new QInfinity::DefaultTextPlugin( this ) )
{
	qDebug() << "Plugin is type " << plugin->infPlugin()->note_type;
	fileModel->addPlugin( *plugin );
	setupActions();
	setupUi();
}

void BrowserMainWindow::slotNewConnection()
{
	NewConnectionDialog *dialog = new NewConnectionDialog( this );
	connect( dialog, SIGNAL(createConnection( const QString&, unsigned int )),
		this, SLOT(slotCreateConnection( const QString&, unsigned int )) );
	dialog->exec();
	delete dialog;
}

void BrowserMainWindow::slotCreateConnection( const QString &hostname,
	unsigned int port )
{
	Connection *conn = new Connection( hostname, port, this );
	connect( conn, SIGNAL(connecting(Connection*)),
		this, SLOT(slotConnectionConnecting(Connection*)) );
	connect( conn, SIGNAL(disconnecting(Connection*)),
		this, SLOT(slotConnectionDisconnecting(Connection*)) );
	connect( conn, SIGNAL(connected(Connection*)),
		this, SLOT(slotConnectionConnected(Connection*)) );
	connect( conn, SIGNAL(error(Connection*, QString)),
		this, SLOT(slotConnectionError(Connection*, QString)) );
	connect( conn, SIGNAL(disconnected(Connection*)),
		this, SLOT(slotConnectionDisconnected(Connection*)) );
	conn->open();
}

void BrowserMainWindow::slotConnectionConnecting( Connection *conn )
{
	QString msg = "Connecting to " + conn->name() + ".";
	statusLabel->setText( msg );
}

void BrowserMainWindow::slotConnectionDisconnecting( Connection *conn )
{
	QString msg = "Disconnecting from " + conn->name() + ".";
	statusLabel->setText( msg );
}

void BrowserMainWindow::slotConnectionConnected( Connection *conn )
{
	qDebug() << "Adding " << conn->name();
	statusLabel->setText( "Connected." );
	fileModel->addConnection( conn->xmppConnection(), conn->name() );
}

void BrowserMainWindow::slotConnectionDisconnected( Connection *conn )
{
	statusLabel->setText( "Disconnected." );
}

void BrowserMainWindow::slotConnectionError( Connection *conn,
	QString message )
{
	QString full_msg = "Error with connection ";
	full_msg += conn->name();
	full_msg += ": ";
	full_msg += message;
	QMessageBox::critical( this, "Connection error", full_msg );
}

void BrowserMainWindow::slotSelectionChanged( const QItemSelection &selected,
	const QItemSelection &deselected )
{
	Q_UNUSED(deselected);
	newConnectionAction->setEnabled( canCreateConnection( selected ) );
	newFolderAction->setEnabled( canCreateFolder( selected ) );
	newNoteAction->setEnabled( canCreateNote( selected ) );
	deleteAction->setEnabled( canDeleteItem( selected ) );
	openAction->setEnabled( canOpenItem( selected ) );
}

void BrowserMainWindow::slotQuit()
{
	exit(0);
}

void BrowserMainWindow::slotCreateFolder()
{
	QModelIndexList indexes = selectedIndexes();
	if( indexes.size() != 1 )
	{
		qDebug() << "Cant create folder without parent.";
		return;
	}
	CreateItemDialog *dialog = new CreateItemDialog( "Create folder named: ",
		this );
	dialog->exec();
	fileModel->createDirectory( indexes[0], dialog->itemName() );
	delete dialog;
}

void BrowserMainWindow::slotCreateNote()
{
	QModelIndexList indexes = selectedIndexes();
	if( indexes.size() != 1 )
	{
		qDebug() << "Can only create notes with one parent.";
		return;
	}
	CreateItemDialog *dialog = new CreateItemDialog( "Create note named: ",
		this );
	dialog->exec();
	fileModel->createNote( indexes[0], *plugin, dialog->itemName() );
	delete dialog;
}

void BrowserMainWindow::slotDelete()
{
	QModelIndexList selected = selectedIndexes();
	QModelIndex index;

	foreach( index, selected )
		fileModel->removeRow( index.row(), index.parent() );
}

void BrowserMainWindow::slotOpen()
{
	QModelIndexList selected = selectedIndexes();
	QModelIndex index;
	QStandardItem *stdItem;
	QInfinity::NodeItem *nodeItem;

	foreach( index, selected )
	{
		stdItem = fileModel->itemFromIndex( index );
		if( stdItem->type() == QInfinity::BrowserItemFactory::NodeItem )
		{
			nodeItem = dynamic_cast<QInfinity::NodeItem*>(stdItem);
			QInfinity::BrowserIter itr = nodeItem->iter();
			itr.browser()->subscribeSession( itr );
		}
	}
}

void BrowserMainWindow::contextMenuEvent( QContextMenuEvent *event )
{
	if( !event )
		return;

	showContextMenu( event->globalPos() );
}

void BrowserMainWindow::setupUi()
{
	treeView = new QTreeView( this );
	treeView->setModel( fileModel );
	connect( treeView, SIGNAL(expanded(const QModelIndex&)),
		fileModel, SLOT(itemActivated(const QModelIndex&)) );
	connect( treeView->selectionModel(), SIGNAL(
			selectionChanged(const QItemSelection&, const QItemSelection&)),
		this, SLOT(slotSelectionChanged(
			const QItemSelection&, const QItemSelection&)) );
	setStatusBar( new QStatusBar( this ) );
	statusLabel = new QLabel();
	statusBar()->addWidget( statusLabel );
	setCentralWidget( treeView );
}

void BrowserMainWindow::setupActions()
{
	quitAction = new QAction( tr("&Quit"), this );
	newConnectionAction = new QAction( tr("&New Connection..."), this );
	newFolderAction = new QAction( tr("Create &Folder..."), this );
	newFolderAction->setEnabled( false );
	newNoteAction = new QAction( tr("Create &Note..."), this );
	newNoteAction->setEnabled( false );
	deleteAction = new QAction( tr("Delete"), this );
	deleteAction->setEnabled( false );
	openAction = new QAction( tr("Open"), this );
	openAction->setEnabled( false );

	connect( quitAction, SIGNAL(triggered(bool)),
		this, SLOT(slotQuit()) );
	connect( newConnectionAction, SIGNAL(triggered(bool)),
		this, SLOT(slotNewConnection()) );
	connect( newFolderAction, SIGNAL(triggered(bool)),
		this, SLOT(slotCreateFolder()) );
	connect( newNoteAction, SIGNAL(triggered(bool)),
		this, SLOT(slotCreateNote()) );
	connect( deleteAction, SIGNAL(triggered(bool)),
		this, SLOT(slotDelete()) );
	connect( openAction, SIGNAL(triggered(bool)),
		this, SLOT(slotOpen()) );

	QMenu *fileMenu = new QMenu( tr("&File"), this );
	fileMenu->addAction( newConnectionAction );
	fileMenu->addAction( quitAction );

	menuBar()->addMenu( fileMenu );
}

void BrowserMainWindow::showContextMenu( const QPoint &globalPos )
{
	static QMenu *contextMenu = 0;
	if( !contextMenu )
	{
		contextMenu = new QMenu( this );
		contextMenu->addAction( newConnectionAction );
		contextMenu->addAction( newFolderAction );
		contextMenu->addAction( newNoteAction );
		contextMenu->addAction( deleteAction );
		contextMenu->addAction( openAction );
	}
	contextMenu->popup( globalPos );
}

bool BrowserMainWindow::canCreateConnection( const QItemSelection &selected )
{
	return true;
}

bool BrowserMainWindow::canCreateFolder( const QItemSelection &selected )
{
	QList<QModelIndex> indexes = selected.indexes();
	if( indexes.size() != 1 )
		return false;
	QStandardItem *item = fileModel->itemFromIndex( indexes[0] );
	if( item->type() != QInfinity::BrowserItemFactory::NodeItem )
		return false;
	return dynamic_cast<QInfinity::NodeItem*>(item)->isDirectory();
}

bool BrowserMainWindow::canCreateNote( const QItemSelection &selected )
{
	return canCreateFolder( selected );
}

bool BrowserMainWindow::canDeleteItem( const QItemSelection &selected )
{
	QList<QModelIndex> indexes = selected.indexes();
	return indexes.size() > 0;
}

bool BrowserMainWindow::canOpenItem( const QItemSelection &selected )
{
	QList<QModelIndex> indexes = selected.indexes();
	return indexes.size() > 0;
}

QModelIndexList BrowserMainWindow::selectedIndexes() const
{
	return treeView->selectionModel()->selection().indexes();
}

QInfinity::NodeItem *BrowserMainWindow::nodeItemFromIndex( const QModelIndex &index ) const
{
	QStandardItem *item = fileModel->itemFromIndex( index );
	if( item->type() != QInfinity::BrowserItemFactory::NodeItem )
	{
		qDebug() << "Cannot convert item of non NodeItem type to NodeItem.";
		return 0;
	}
	return dynamic_cast<QInfinity::NodeItem*>(item);
}

int main( int argc, char **argv )
{
	QInfinity::init();
	QApplication app( argc, argv );
	BrowserMainWindow mainWindow;
	mainWindow.setVisible( true );

	app.exec();
	QInfinity::deinit();
	return 0;
}