File: userfiledialog.cpp

package info (click to toggle)
dc-qt 0.2.0.alpha-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,936 kB
  • ctags: 5,361
  • sloc: cpp: 28,936; makefile: 19
file content (148 lines) | stat: -rw-r--r-- 4,029 bytes parent folder | download | duplicates (4)
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
/*
 *  userfiledialog.cpp
 *  ui
 *
 *  Created by Mikael Gransell on 4/26/06.
 *  Copyright 2006 __MyCompanyName__. All rights reserved.
 *
 */

#include "log.h"
#include "userfiledialog.h"
#include "globalusermodel.h"


UserFileDialog::UserFileDialog( const UserFileModelPtr& mdl,
								BackendConnectionPtr backendConn,
								boost::shared_ptr<GlobalUserModel> userModel,
								QWidget* parent )
: QDialog(parent),
  contextMenu(new QMenu(this)),
  backendConnection(backendConn),
  model( mdl )
{
	  ui.setupUi(this);
	  ui.treeView->setModel( model.get() );
	  
	  setWindowTitle(userModel->getUser(model->getUserId())->nick);
	  
	  createMenu();
}

void UserFileDialog::on_treeView_customContextMenuRequested( const QPoint& pos )
{
	// Show the menu
	contextMenu->popup( ui.treeView->mapToGlobal(pos) );
}

void UserFileDialog::on_treeView_doubleClicked()
{
	logger->debug( "Double click download" );
    // Try to get all the selected indexes and issue a download for each one.
	QModelIndexList selected = ui.treeView->selectionModel()->selectedIndexes();
	for( int i = 0; i < selected.size() - 1; i++ ) {
		QModelIndex index = selected[i];
		if( !model->getItem(index)->isDir() ) {
			download( index );
		}
	}
}

void UserFileDialog::onDownload()
{
	logger->debug( "Download triggered" );
    // Try to get all the selected indexes and issue a download for each one.
	QModelIndexList selected = ui.treeView->selectionModel()->selectedIndexes();
	for( int i = 0; i < selected.size() - 1; i++ ) {
		download( selected[i] );
	}	
}

void UserFileDialog::onDownloadTo()
{
	logger->debug( "DownloadTo triggered" );
    QString dir = QFileDialog::getExistingDirectory( this, tr("Choose a download directory") );
	if( !dir.isEmpty() ){
		dir += "/";
		// Try to get all the selected indexes and issue a download for each one.
		QModelIndexList selected = ui.treeView->selectionModel()->selectedIndexes();
		for( int i = 0; i < selected.size() - 1; i++ ) {
			downloadTo( selected[i], dir );
		}
	}
}


void UserFileDialog::download( const QModelIndex& index )
{
	try {
		// Throws std::out_of_range
		UserFileModel::TreeItem* item = model->getItem( index );
		
		if ( item ) {
			QString file = formatFileName( item );
			
			int64 size = item->isDir() ? -1 : item->getSize();
			QString tth = item->getTTH();
			
			logger->debug( "Downloading: " + file + " with size " + QString::number(size) + " and tth:" + tth );
			
			backendConnection->downloadFile( model->getUserId(), 
											 file, 
											 size, 
											 tth );
		}
	}
	catch(const std::out_of_range& e) {
		logger->error("Bad index when trying to download file\n");
	}
}

void UserFileDialog::downloadTo( const QModelIndex& index, const QString& path )
{
	try {
		// Throws std::out_of_range
		UserFileModel::TreeItem* item = model->getItem( index );
		
		if ( item ) {
			QString file = formatFileName( item );
			
			int64 size = item->isDir() ? -1 : item->getSize();
			QString tth = item->getTTH();
			
			logger->debug( "Downloading: " + file + " with size " + QString::number(size) + " and tth:" + tth + " to " + path );
			
			backendConnection->downloadFileTo( model->getUserId(), 
											   file, 
											   size, 
											   tth, 
											   path );
		}
	}
	catch(const std::out_of_range& e) {
		logger->error("Bad index when trying to download file\n");
	}	
}

QString UserFileDialog::formatFileName( const UserFileModel::TreeItem* item ) const
{
	QString file = item->getFullName();
	// Strip the first part of the path since this is the user name
	file = file.section( '/', 2 );
	
	// If this is a directory we need to add the final /
	if ( item->isDir() ) {
		file += '/';
	}
	return file;
}

void UserFileDialog::createMenu()
{
	QAction* downloadAct = contextMenu->addAction( tr("Download") );
	connect(downloadAct, SIGNAL(triggered()), this, SLOT(onDownload()));
	
	QAction* downloadToAct = contextMenu->addAction( tr("Download to...") );
	connect(downloadToAct, SIGNAL(triggered()), this, SLOT(onDownloadTo()));
}