~moshe-wagner/orayta/Orayta-QT

« back to all changes in this revision

Viewing changes to importbook.cpp

  • Committer: moshe.wagner
  • Date: 2012-05-05 21:48:52 UTC
  • Revision ID: git-v1:70e09345355d8f7ecaf4ec2176a47d02dea9bcb7
Making the android branch into the main one

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* This program is free software; you can redistribute it and/or modify
 
2
* it under the terms of the GNU General Public License version 2
 
3
* as published by the Free Software Foundation.
 
4
*
 
5
* This program is distributed in the hope that it will be useful,
 
6
* but WITHOUT ANY WARRANTY; without even the implied warranty of
 
7
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
8
* GNU General Public License for more details.
 
9
*
 
10
* You should have received a copy of the GNU General Public License
 
11
* along with this program; if not, write to the Free Software
 
12
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 
13
*
 
14
* Author: Moshe Wagner. <moshe.wagner@gmail.com>
 
15
*/
 
16
 
 
17
 
 
18
#include "importbook.h"
 
19
#include "functions.h"
 
20
#include "ui_importbook.h"
 
21
#ifndef MOBILE
 
22
#include "desktopapp.h"
 
23
#endif
 
24
 
 
25
 
 
26
#include <QDesktopServices>
 
27
#include <QDebug>
 
28
#include <QUrl>
 
29
#include <QFileDialog>
 
30
 
 
31
 
 
32
importBook::importBook(QWidget *parent) : QDialog(parent), ui(new Ui::importBook)
 
33
{
 
34
    ui->setupUi(this);
 
35
 
 
36
    ui->listWidget->setSelectionMode(QAbstractItemView::ExtendedSelection);
 
37
    ui->cancelBTN->hide();
 
38
    ui->importBTN->hide();
 
39
    ui->deleteButton->hide();
 
40
}
 
41
 
 
42
importBook::~importBook()
 
43
{
 
44
    delete ui;
 
45
}
 
46
 
 
47
void importBook::on_addFolder_clicked()
 
48
{
 
49
    QString dirName = QFileDialog::getExistingDirectory(this, "", QDir::homePath(), QFileDialog::ShowDirsOnly);
 
50
 
 
51
    if (!dirName.isEmpty())
 
52
    {
 
53
        ui->listWidget->addItem(dirName);
 
54
        ui->cancelBTN->show();
 
55
        ui->importBTN->show();
 
56
        ui->deleteButton->show();
 
57
    }
 
58
}
 
59
 
 
60
void importBook::on_addBooks_clicked()
 
61
{
 
62
    QString filters = tr("All supported files "
 
63
                         "(*.html *.htm *.txt *.pdf);;"
 
64
                         "Html files(*.htm *.html);;"
 
65
                         "Text files(*.txt);;"
 
66
                         "Pdf files(*.pdf)");
 
67
 
 
68
    QStringList fileNames = QFileDialog::getOpenFileNames(this, "", QDir::homePath(), filters);
 
69
 
 
70
    if (!fileNames.empty())
 
71
    {
 
72
        for (int i=0; i<fileNames.size(); i++)
 
73
        {
 
74
            ui->listWidget->addItem(fileNames[i]);
 
75
        }
 
76
        ui->cancelBTN->show();
 
77
        ui->importBTN->show();
 
78
        ui->deleteButton->show();
 
79
    }
 
80
}
 
81
 
 
82
void importBook::on_deleteButton_clicked()
 
83
{
 
84
    QList<QListWidgetItem *> selected_items = ui->listWidget->selectedItems();
 
85
    for (int i=0; i < selected_items.size(); i++)
 
86
    {
 
87
        delete selected_items[i];
 
88
    }
 
89
 
 
90
    if ( ui->listWidget->count() == 0 )
 
91
        ui->deleteButton->hide();
 
92
}
 
93
 
 
94
void importBook::on_label_2_linkActivated(QString link)
 
95
{
 
96
    //Open the project's site in a browser
 
97
    QDesktopServices::openUrl(QUrl(link));
 
98
}
 
99
 
 
100
void importBook::on_cancelBTN_clicked()
 
101
{
 
102
    close();
 
103
}
 
104
 
 
105
void importBook::on_importBTN_clicked()
 
106
{
 
107
    QString booksUserPath = USERPATH + "Books/ספרי_המשתמש";
 
108
 
 
109
    //Make sure the dir exists
 
110
    QDir d(booksUserPath) ;
 
111
    if ( !d.exists() )
 
112
        d.mkpath(booksUserPath);
 
113
 
 
114
    booksUserPath = booksUserPath + "/";
 
115
 
 
116
    QStringList filters;
 
117
    filters << "*.html" << "*.htm" << "*.pdf" << "*.txt";
 
118
 
 
119
 
 
120
 
 
121
    //Copy to user's book folder
 
122
    for (int i=0; i<ui->listWidget->count(); i++)
 
123
    {
 
124
        QFileInfo f( ui->listWidget->item(i)->text() );
 
125
 
 
126
        if ( f.exists() )
 
127
        {
 
128
            //Copy whole directory
 
129
            if( f.isDir() )
 
130
            {
 
131
                copyFolder( f.absoluteFilePath(), booksUserPath + f.fileName(), filters);
 
132
            }
 
133
            else //Copy file
 
134
            {
 
135
                QFile cf;
 
136
 
 
137
                if ( !cf.copy(f.absoluteFilePath(), booksUserPath + f.fileName()) )
 
138
                    qDebug() << "Can't copy file" << f.absoluteFilePath();
 
139
            }
 
140
        }
 
141
    }
 
142
 
 
143
    emit updateTree();
 
144
 
 
145
    close();
 
146
}