summaryrefslogtreecommitdiffstats
path: root/konversation/src/urlcatcher.cpp
blob: 0c90533679cb7904f6c4b708ff6f8ef66f0e1af4 (plain)
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
/*
  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation; either version 2 of the License, or
  (at your option) any later version.
*/

/*
  shows all URLs found by the client
  begin:     Die Mai 27 2003
  copyright: (C) 2003 by Dario Abatianni
  email:     eisfuchs@tigress.com
*/

#include "urlcatcher.h"
#include "channel.h"
#include "server.h"
#include "konversationapplication.h"
#include "viewcontainer.h"

#include <tqhbox.h>
#include <tqpushbutton.h>
#include <tqregexp.h>
#include <tqclipboard.h>
#include <tqwhatsthis.h>
#include <tqlayout.h>

#include <tdeapplication.h>
#include <tdeactionclasses.h>
#include <tdelocale.h>
#include <kdebug.h>
#include <tdelistview.h>
#include <krun.h>
#include <tdefiledialog.h>
#include <kprocess.h>
#include <tdeversion.h>
#include <kshell.h>
#include <tdelistviewsearchline.h>


UrlCatcher::UrlCatcher(TQWidget* parent) : ChatWindow(parent)
{
    layout()->setAutoAdd(false);
    setName(i18n("URL Catcher"));
    setType(ChatWindow::UrlCatcher);

    urlListView=new TDEListView(this,"url_list_view");
    urlListView->addColumn(i18n("Nick"));
    urlListView->addColumn(i18n("URL"));
    urlListView->setFullWidth(true);
    urlListView->setAllColumnsShowFocus(true);
    TQString urlListViewWT = i18n(
        "List of Uniform Resource Locators mentioned in any of the Konversation windows "
        "during this session.");
    TQWhatsThis::add(urlListView, urlListViewWT);

    searchWidget = new TDEListViewSearchLineWidget(urlListView, this, "search_line");
    searchWidget->setEnabled(false);

    TQHBox* buttonBox=new TQHBox(this);
    buttonBox->setSpacing(spacing());

    openUrlButton=new TQPushButton(i18n("&Open URL"),buttonBox,"open_url_button");
    TQString openUrlButtonWT = i18n(
        "<p>Select a <b>URL</b> above, then click this button to launch the "
        "application associated with the mimetype of the URL.</p>"
        "<p>In the <b>Settings</b>, under <b>Behavior</b> | <b>General</b>, "
        "you can specify a custom web browser for web URLs.</p>");
    TQWhatsThis::add(openUrlButton, openUrlButtonWT);
    copyUrlButton=new TQPushButton(i18n("&Copy URL"),buttonBox,"copy_url_button");
    TQString copyUrlButtonWT = i18n(
        "Select a <b>URL</b> above, then click this button to copy the URL to the clipboard.");
    TQWhatsThis::add(copyUrlButton, copyUrlButtonWT);
    deleteUrlButton=new TQPushButton(i18n("&Delete URL"),buttonBox,"delete_url_button");
    TQString deleteUrlButtonWT = i18n(
        "Select a <b>URL</b> above, then click this button to delete the URL from the list.");
    TQWhatsThis::add(deleteUrlButton, deleteUrlButtonWT);
    saveListButton=new TQPushButton(i18n("Sa&ve List..."),buttonBox,"save_list_button");
    TQString saveListButtonWT = i18n(
        "Click to save the entire list to a file.");
    TQWhatsThis::add(saveListButton, saveListButtonWT);
    clearListButton=new TQPushButton(i18n("C&lear List"),buttonBox,"clear_list_button");
    TQString clearListButtonWT = i18n(
        "Click to erase the entire list.");
    TQWhatsThis::add(clearListButton, clearListButtonWT);

    connect(urlListView,TQT_SIGNAL (executed(TQListViewItem*)),this,TQT_SLOT (openUrl(TQListViewItem*)) );
    connect(urlListView,TQT_SIGNAL (selectionChanged()),this,TQT_SLOT (urlSelected()) );

    connect(openUrlButton,TQT_SIGNAL (clicked()),this,TQT_SLOT (openUrlClicked()) );
    connect(copyUrlButton,TQT_SIGNAL (clicked()),this,TQT_SLOT (copyUrlClicked()) );
    connect(deleteUrlButton,TQT_SIGNAL (clicked()),this,TQT_SLOT (deleteUrlClicked()) );
    connect(saveListButton,TQT_SIGNAL (clicked()),this,TQT_SLOT (saveListClicked()) );
    connect(clearListButton,TQT_SIGNAL (clicked()),this,TQT_SLOT (clearListClicked()) );

    saveListButton->setEnabled(false);
    clearListButton->setEnabled(false);

    layout()->add(searchWidget);
    layout()->add(urlListView);
    layout()->add(buttonBox);

    urlSelected();
}


UrlCatcher::~UrlCatcher()
{
}

void UrlCatcher::urlSelected()
{
    TQListViewItem* item=urlListView->selectedItem();
    if(item)
    {
        openUrlButton->setEnabled(true);
        copyUrlButton->setEnabled(true);
        deleteUrlButton->setEnabled(true);
    }
    else
    {
        openUrlButton->setEnabled(false);
        copyUrlButton->setEnabled(false);
        deleteUrlButton->setEnabled(false);
    }
}

void UrlCatcher::addUrl(const TQString& who,const TQString& url)
{
    new TDEListViewItem(urlListView,who,url);
    clearListButton->setEnabled(true);
    saveListButton->setEnabled(true);
    searchWidget->setEnabled(true);
}

void UrlCatcher::openUrl(TQListViewItem* item)
{
    TQString url = item->text(1);
    if (!Preferences::useCustomBrowser() || url.lower().startsWith("mailto:") )
    {
        new KRun(KURL(url));
    }
    else
    {
        TQString cmd = Preferences::webBrowserCmd();
        cmd.replace("%u", url);
        TDEProcess *proc = new TDEProcess;
        TQStringList cmdAndArgs = KShell::splitArgs(cmd);
        *proc << cmdAndArgs;
        //    This code will also work, but starts an extra shell process.
        //    kdDebug() << "UrlCatcher::openUrl(): cmd = " << cmd << endl;
        //    *proc << cmd;
        //    proc->setUseShell(true);
        proc->start(TDEProcess::DontCare);
        delete proc;
    }
}

void UrlCatcher::openUrlClicked()
{
    TQListViewItem* item=urlListView->selectedItem();
    if(item) openUrl(item);
}

void UrlCatcher::copyUrlClicked()
{
    TQListViewItem* item=urlListView->selectedItem();
    if(item)
    {
        TQClipboard *cb=TDEApplication::kApplication()->clipboard();
        cb->setText(item->text(1),TQClipboard::Selection);
        cb->setText(item->text(1),TQClipboard::Clipboard);
    }
}

void UrlCatcher::deleteUrlClicked()
{
    TQListViewItem* item=urlListView->selectedItem();
    if(item)
    {
        emit deleteUrl(item->text(0),item->text(1));
        delete item;
        // select next item
        item=urlListView->currentItem();
        if(item) urlListView->setSelected(item,true);
        else
        {
            saveListButton->setEnabled(false);
            clearListButton->setEnabled(false);
            searchWidget->setEnabled(false);
        }
    }
}

void UrlCatcher::clearListClicked()
{
    urlListView->clear();
    saveListButton->setEnabled(false);
    clearListButton->setEnabled(false);
    urlSelected();
    emit clearUrlList();
}

void UrlCatcher::saveListClicked()
{
    // Ask user for file name
    TQString fileName=KFileDialog::getSaveFileName(
        TQString(),
        TQString(),
        this,
        i18n("Save URL List"));

    if(!fileName.isEmpty())
    {
        // now save the list to disk
        TQFile listFile(fileName);
        listFile.open(IO_WriteOnly);
        // wrap the file into a stream
        TQTextStream stream(&listFile);
        TQListViewItem* item=urlListView->itemAtIndex(0);
        while(item)
        {
            stream << item->text(0) << ": " << item->text(1) << endl;
            item=item->itemBelow();
        }                                         // while
    }
}

void UrlCatcher::childAdjustFocus()
{
}

#include "urlcatcher.moc"