~moshe-wagner/orayta/Orayta-QT

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
321
322
323
324
325
326
327
328
329
330
/* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2
* as published by the Free Software Foundation.
*
* 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 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
*
* Author: Moshe Wagner. <moshe.wagner@gmail.com>
*/

#ifndef MOBILE
#include "desktopapp.h"
#include "bookmarktitle.h"
#include "bookdisplayer.h"
#include "mywebview.h"
#endif



// This is just a very simple define. every place in the code,
//  "CURRENT_TAB" simply represents "ui->viewTab->currentIndex()".
#define CURRENT_TAB ui->viewTab->currentIndex()


void DesktopApp::on_bookmarkWidget_customContextMenuRequested(QPoint pos)
{
//    int row = ui->bookmarkWidget->currentRow();

    QMenu menu(ui->treeWidget);

    QAction *edit = new QAction(tr("Edit bookmark title..."), &menu);
    edit->setIcon(QIcon(":/Icons/edit.png"));

    QAction *del = new QAction(tr("Delete bookmark"), &menu);
    del->setIcon(QIcon(":/Icons/edit-delete.png"));

    //Im to lazy to do this the right way...
    QObject::connect(edit, SIGNAL(triggered()), this , SLOT(on_bookmarkEdit_clicked()));
    QObject::connect(del, SIGNAL(triggered()), this , SLOT(on_removeBookmark_clicked()));

    menu.addAction(edit);
    menu.addAction(del);

    menu.setLayoutDirection(Qt::RightToLeft);

    //Open the menu to the left of the cursor's position
    QPoint point = QPoint(QCursor::pos().x() - menu.width(), QCursor::pos().y());
    menu.exec(point);
}

QString DesktopApp::bookMarkTitle(QString lnk)
{
    //Format position for human display
    int p = lnk.indexOf(":");
    QString lnkdisplay = lnk.mid(p+1);
    lnkdisplay = unescapeFromBase32(lnkdisplay).replace("_", " ").replace("-", " ").replace("{", "").replace("}", "").replace(" 0", "");

    int id;
    ToNum(lnk.mid(0,p), &id);

    //Find and add the book's name
    int index = bookList.FindBookById(id);
    if (index == -1)
    {
        print ("Invalid link!");
        return "";
    }
    else lnkdisplay = bookList[index]->getNormallDisplayName() + " " + lnkdisplay;

    //Remove extra spaces that appear for some reason:
    lnkdisplay.replace(QRegExp("[ ][ ]*"), " ");

    return lnkdisplay;
}

//Opens the bookmark dialog
void DesktopApp::bookMarkPosition(QString lnk)
{
    QString linkdisplay = bookMarkTitle(lnk);

    BookMarkTitle *t = new BookMarkTitle(this, lnk, linkdisplay);
    t->show();

    connect (t, SIGNAL(OK(QString,QString)), this, SLOT(addBookMark(QString,QString)));
}

//Adds the given book position to the bookmark list and file
void DesktopApp::addBookMark(QString link, QString title)
{
    QString linkdisplay = bookMarkTitle(link);
    if ( linkdisplay == title) title = "";

    addBookMarkToList(link, title);

    //Write to bookmark file
    writetofile(USERPATH + "BookMarkList.txt", link + "|" + title + "\n", "UTF-8", false);
}


//Adds a bookmark to the bookmark list, (giving it's encoded link)
void DesktopApp::addBookMarkToList(QString lnk, QString title)
{
    QListWidgetItem *item = new QListWidgetItem(ui->bookmarkWidget);

    QString linkdisplay = bookMarkTitle(lnk);

    if ( title == "")
    {
        item->setText( linkdisplay);
        title = linkdisplay;
    }
    else item->setText( title + " (" + linkdisplay + ")" );

    //This is clearly not the right way to do this, but hey, it works.
    item->setData(Qt::StatusTipRole, lnk);
    item->setData(Qt::WhatsThisRole, title);

    item->setIcon(QIcon(":Icons/bookmarks.png"));
}

//Build the bookmark list from the bookmark file
void DesktopApp::buildBookMarkList()
{
    QList <QString> text;
    ReadFileToList(USERPATH + "BookMarkList.txt", text, "UTF-8");

    for ( int i=0; i<text.size(); i++)
    {
        QString title = "";
        //Split title and link
        int p = text[i].indexOf("|"); //TODO: escape this guy
        if ( p != -1) title = text[i].mid(p+1);
        addBookMarkToList(text[i].mid(0,p), title);
    }
}

//Open the clicked bookmark
void DesktopApp::on_bookmarkWidget_itemDoubleClicked(QListWidgetItem* item)
{
    QString lnk = item->data(Qt::StatusTipRole).toString();

    vector<QString> parts;
    splittotwo(lnk, parts, ":");
    int id;
    if(ToNum(parts[0], &id))
    {
        Book* book = bookList.findBookById(id);
        if( book )
        {
            QWidget* tabWidget = book->tabWidget();
            if ( tabWidget != 0 )
            {
                ui->viewTab->setCurrentWidget (tabWidget);

                CurrentBookdisplayer()->webview()->page()->mainFrame()->scrollToAnchor("#" + parts[1]);

                CurrentBookdisplayer()->execScript(QString("paintByHref(\"$" + parts[1] + "\");"));
            }
            else
            {
                //Add a new tab and open the link there
                addViewTab(false);
                ui->viewTab->setTabText(CURRENT_TAB, tr("Orayta"));

                CurrentBookdisplayer()->setInternalLocation("#" + parts[1]);
                openBook(book);
            }
        }
    }
}

//Opens a dialog allowing to edit the selected bookmark's title
void DesktopApp::on_bookmarkEdit_clicked()
{
    if (ui->bookmarkWidget->currentIndex().isValid())
    {
        int ind = ui->bookmarkWidget->currentIndex().row();

        QString otitle = ui->bookmarkWidget->item(ind)->data(Qt::WhatsThisRole).toString();

        //Open the bookmark title giving dialog with this index and title
        BookMarkTitle *t = new BookMarkTitle(this, ind, otitle);
        t->show();

        //The dialog will call the editBookMarkTitle function when it's done
        connect (t, SIGNAL(RenameOK(int, QString)), this, SLOT(editBookMarkTitle(int, QString)));
    }
}

//Changes the title of the given bookmark (by it's index) to the given one
void DesktopApp::editBookMarkTitle(int ind, QString newtitle)
{
    QString link = ui->bookmarkWidget->item(ind)->data(Qt::StatusTipRole).toString();

    QString otitle = ui->bookmarkWidget->item(ind)->data(Qt::WhatsThisRole).toString();
    if (otitle == bookMarkTitle(link)) otitle = "";


    //Edit the item title data (it's saved here...) to the new one
    ui->bookmarkWidget->item(ind)->setData(Qt::WhatsThisRole, newtitle);

    //Update file:
    QList <QString> text;
    ReadFileToList(USERPATH + "BookMarkList.txt", text, "UTF-8");

    QString str = "";
    for (int i=0; i<text.size(); i++)
    {
        if ( i != ind) str += text[i] + "\n";
        else str += link + "|" + newtitle + "\n";
    }

    writetofile(USERPATH + "BookMarkList.txt", str, "UTF-8", true);

    //Uodate the new title in the list
    if (newtitle != "")
    {
        //Replace the titles
        if (otitle != "") ui->bookmarkWidget->item(ind)->setText( ui->bookmarkWidget->item(ind)->text().replace(otitle, newtitle) );
        //If there was an empty title before, add the newtitle and "("-s
        else ui->bookmarkWidget->item(ind)->setText( newtitle + " (" + bookMarkTitle(link) + ")");
    }
    //Empty title given
    else
    {
        //Remove the title and "("-s
        if (otitle != "") ui->bookmarkWidget->item(ind)->setText( ui->bookmarkWidget->item(ind)->text().replace(otitle + " (", "").replace(QRegExp("[)]$"), ""));
    }
}


//Remove the selected bookmark from the list and file
void DesktopApp::on_removeBookmark_clicked()
{
    int ind = ui->bookmarkWidget->currentIndex().row();

    QList <QString> text;
    ReadFileToList(USERPATH + "BookMarkList.txt", text, "UTF-8");

    QString str = "";
    for (int i=0; i<text.size(); i++)
    {
        if ( i != ind) str += text[i] + "\n";
    }

    writetofile(USERPATH + "BookMarkList.txt", str, "UTF-8", true);

    ui->bookmarkWidget->takeItem( ind );
}


//Move the selected bookmark up in the list by one position
void DesktopApp::on_bookmarkUp_clicked()
{
    int ind = ui->bookmarkWidget->currentIndex().row();

    if (ind > 0)
    {
        QList <QString> text;
        ReadFileToList(USERPATH + "BookMarkList.txt", text, "UTF-8");

        swap(text[ind], text[ind-1]);

        QString str = "";
        for (int i=0; i<text.size(); i++) str += text[i] + "\n";

        writetofile(USERPATH + "BookMarkList.txt", str, "UTF-8", true);

        //Swap the list elements:
        QListWidgetItem *tmp = ui->bookmarkWidget->currentItem();
        ui->bookmarkWidget->takeItem( ind );
        ui->bookmarkWidget->insertItem( ind - 1 ,tmp);
        ui->bookmarkWidget->setCurrentRow( ind - 1 );
    }
}

//Move the selected bookmark down in the list by one position
void DesktopApp::on_bookmarkDown_clicked()
{
    int ind = ui->bookmarkWidget->currentIndex().row();

    if (ind < ui->bookmarkWidget->count() - 1 )
    {
        QList <QString> text;
        ReadFileToList(USERPATH + "BookMarkList.txt", text, "UTF-8");

        swap(text[ind], text[ind+1]);

        QString str = "";
        for (int i=0; i<text.size(); i++) str += text[i] + "\n";

        writetofile(USERPATH + "BookMarkList.txt", str, "UTF-8", true);

        //Swap the list elements:
        QListWidgetItem *tmp = ui->bookmarkWidget->currentItem();
        ui->bookmarkWidget->takeItem( ind );
        ui->bookmarkWidget->insertItem( ind + 1 ,tmp);
        ui->bookmarkWidget->setCurrentRow( ind + 1 );
    }
}

void DesktopApp::menuBookMark()
{
    QString link = CurrentBookdisplayer()->activeLink().replace("$","");

    //Find book's id and add it to the link
    int id = CurrentBookdisplayer()->book()->getUniqueId();
    link = stringify(id) + ":" + link;

    bookMarkPosition( link );
}

void DesktopApp::on_addBookMark_clicked()
{
    QString link = CurrentBookdisplayer()->activeLink().replace("$","");

    //Find book's id and add it to the link
    int id = CurrentBookdisplayer()->book()->getUniqueId();
    link = stringify(id) + ":" + link;

    if (id != -1) addBookMark(link, ui->bookmarkTitleLine->text());

    ui->bookmarkTitleLine->clear();
}