examples/network/torrent/addtorrentdialog.cpp
changeset 0 1918ee327afb
child 3 41300fa6a67c
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 #include "addtorrentdialog.h"
       
    43 #include "metainfo.h"
       
    44 
       
    45 #include <QFile>
       
    46 #include <QFileDialog>
       
    47 #include <QLineEdit>
       
    48 #include <QMetaObject>
       
    49 
       
    50 static QString stringNumber(qint64 number)
       
    51 {
       
    52     QString tmp;
       
    53     if (number > (1024 * 1024 * 1024))
       
    54         tmp.sprintf("%.2fGB", number / (1024.0 * 1024.0 * 1024.0));
       
    55     else if (number > (1024 * 1024))
       
    56         tmp.sprintf("%.2fMB", number / (1024.0 * 1024.0));
       
    57     else if (number > (1024))
       
    58         tmp.sprintf("%.2fKB", number / (1024.0));
       
    59     else
       
    60         tmp.sprintf("%d bytes", int(number));
       
    61     return tmp;
       
    62 }
       
    63 
       
    64 AddTorrentDialog::AddTorrentDialog(QWidget *parent)
       
    65   : QDialog(parent, Qt::Sheet)
       
    66 {
       
    67     ui.setupUi(this);
       
    68 
       
    69     connect(ui.browseTorrents, SIGNAL(clicked()),
       
    70             this, SLOT(selectTorrent()));
       
    71     connect(ui.browseDestination, SIGNAL(clicked()),
       
    72             this, SLOT(selectDestination()));
       
    73     connect(ui.torrentFile, SIGNAL(textChanged(const QString &)),
       
    74             this, SLOT(setTorrent(const QString &)));
       
    75 
       
    76     ui.destinationFolder->setText(destinationDirectory = QDir::current().path());
       
    77     ui.torrentFile->setFocus();
       
    78 }
       
    79 
       
    80 void AddTorrentDialog::selectTorrent()
       
    81 {
       
    82     QString fileName = QFileDialog::getOpenFileName(this, tr("Choose a torrent file"),
       
    83                                                     lastDirectory,
       
    84                                                     tr("Torrents (*.torrent);; All files (*.*)"));
       
    85     if (fileName.isEmpty())
       
    86         return;
       
    87     lastDirectory = QFileInfo(fileName).absolutePath();
       
    88     setTorrent(fileName);
       
    89 }
       
    90 
       
    91 void AddTorrentDialog::selectDestination()
       
    92 {
       
    93     QString dir = QFileDialog::getExistingDirectory(this, tr("Choose a destination directory"),
       
    94                                                     lastDestinationDirectory);
       
    95     if (dir.isEmpty())
       
    96         return;
       
    97     lastDestinationDirectory = destinationDirectory = dir;
       
    98     ui.destinationFolder->setText(destinationDirectory);
       
    99     enableOkButton();
       
   100 }
       
   101 
       
   102 void AddTorrentDialog::enableOkButton()
       
   103 {
       
   104     ui.okButton->setEnabled(!ui.destinationFolder->text().isEmpty()
       
   105                             && !ui.torrentFile->text().isEmpty());
       
   106 }
       
   107 
       
   108 void AddTorrentDialog::setTorrent(const QString &torrentFile)
       
   109 {
       
   110     if (torrentFile.isEmpty()) {
       
   111         enableOkButton();
       
   112         return;
       
   113     }
       
   114 
       
   115     ui.torrentFile->setText(torrentFile);
       
   116     if (!torrentFile.isEmpty())
       
   117         lastDirectory = QFileInfo(torrentFile).absolutePath();
       
   118 
       
   119     if (lastDestinationDirectory.isEmpty())
       
   120         lastDestinationDirectory = lastDirectory;
       
   121     
       
   122     MetaInfo metaInfo;
       
   123     QFile torrent(torrentFile);
       
   124     if (!torrent.open(QFile::ReadOnly) || !metaInfo.parse(torrent.readAll())) {
       
   125         enableOkButton();
       
   126         return;
       
   127     }
       
   128     
       
   129     ui.torrentFile->setText(torrentFile);
       
   130     ui.announceUrl->setText(metaInfo.announceUrl());
       
   131     if (metaInfo.comment().isEmpty())
       
   132         ui.commentLabel->setText("<unknown>");
       
   133     else
       
   134         ui.commentLabel->setText(metaInfo.comment());
       
   135     if (metaInfo.createdBy().isEmpty())
       
   136         ui.creatorLabel->setText("<unknown>");
       
   137     else
       
   138         ui.creatorLabel->setText(metaInfo.createdBy());
       
   139     ui.sizeLabel->setText(stringNumber(metaInfo.totalSize()));
       
   140     if (metaInfo.fileForm() == MetaInfo::SingleFileForm) {
       
   141         ui.torrentContents->setHtml(metaInfo.singleFile().name);
       
   142     } else {
       
   143         QString html;
       
   144         foreach (MetaInfoMultiFile file, metaInfo.multiFiles()) {
       
   145             QString name = metaInfo.name();
       
   146             if (!name.isEmpty()) {
       
   147                 html += name;
       
   148                 if (!name.endsWith('/'))
       
   149                     html += '/';
       
   150             }
       
   151             html += file.path + "<br>";
       
   152         }
       
   153         ui.torrentContents->setHtml(html);
       
   154     }
       
   155 
       
   156     QFileInfo info(torrentFile);
       
   157     ui.destinationFolder->setText(info.absolutePath());
       
   158 
       
   159     enableOkButton();
       
   160 }
       
   161 
       
   162 QString AddTorrentDialog::torrentFileName() const
       
   163 {
       
   164     return ui.torrentFile->text();
       
   165 }
       
   166 
       
   167 QString AddTorrentDialog::destinationFolder() const
       
   168 {
       
   169     return ui.destinationFolder->text();
       
   170 }