summaryrefslogtreecommitdiffstats
path: root/konqueror/keditbookmarks/bookmarkinfo.cpp
blob: 38e499d1dd034daec44efe0fc271f1355703c22b (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
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
// vim: set ts=4 sts=4 sw=4 et:
/* This file is part of the KDE project
   Copyright (C) 2003 Alexander Kellett <lypanov@kde.org>

   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; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include "bookmarkinfo.h"
#include "commands.h"
#include "toplevel.h"

#include <stdlib.h>

#include <tqtimer.h>
#include <clipboard.h>
#include <tqsplitter.h>
#include <layout.h>
#include <tqlabel.h>

#include <klocale.h>
#include <kdebug.h>

#include <kapplication.h>
#include <kstdaction.h>
#include <kaction.h>
#include <dcopclient.h>
#include <dcopref.h>

#include <kkeydialog.h>
#include <kedittoolbar.h>
#include <kmessagebox.h>
#include <klineedit.h>
#include <kfiledialog.h>

#include <kbookmarkdrag.h>
#include <kbookmarkmanager.h>

// SHUFFLE all these functions around, the order is just plain stupid

BookmarkLineEdit::BookmarkLineEdit( TQWidget *parent )
    : KLineEdit( parent )
{
}

void BookmarkLineEdit::cut()
{
    TQString select( selectedText() );
    int pos( selectionStart() );
    TQString newText(  text().remove( pos, select.length() ) );
    KLineEdit::cut();
    setEdited( true ); //KDE 4 setModified( true );
    emit textChanged( newText );
    setText( newText );
}


void BookmarkInfoWidget::showBookmark(const KBookmark &bk) {
    commitChanges();
    m_bk = bk;

    if (m_bk.isNull()) {
        // all read only and blank

        m_title_le->setReadOnly(true);
        m_title_le->setText(TQString::null);

        m_url_le->setReadOnly(true);
        m_url_le->setText(TQString::null);

        m_comment_le->setReadOnly(true);
        m_comment_le->setText(TQString::null);

        m_visitdate_le->setReadOnly(true);
        m_visitdate_le->setText(TQString::null);

        m_credate_le->setReadOnly(true);
        m_credate_le->setText(TQString::null);

        m_visitcount_le->setReadOnly(true);
        m_visitcount_le->setText(TQString::null);

        return;
    }

    // read/write fields
    m_title_le->setReadOnly( (bk.isSeparator()|| !bk.hasParent() )? true : false);
    m_title_le->setText(bk.fullText());

    m_url_le->setReadOnly(bk.isGroup() || bk.isSeparator());
    m_url_le->setText(bk.isGroup() ? TQString::null : bk.url().pathOrURL());

    m_comment_le->setReadOnly((bk.isSeparator()|| !bk.hasParent()) ? true : false );
    m_comment_le->setText(
            NodeEditCommand::getNodeText(bk, TQStringList() << "desc"));

    // readonly fields
    updateStatus();
 
}

void BookmarkInfoWidget::updateStatus()
{
   TQString visitDate =
        CurrentMgr::makeTimeStr( NodeEditCommand::getNodeText(m_bk, TQStringList() << "info" << "metadata"
                                                             << "time_visited" ));
    m_visitdate_le->setReadOnly(true);
    m_visitdate_le->setText(visitDate);

    TQString creationDate =
        CurrentMgr::makeTimeStr( NodeEditCommand::getNodeText(m_bk, TQStringList() << "info" << "metadata"
                                                             << "time_added" ));
    m_credate_le->setReadOnly(true);
    m_credate_le->setText(creationDate);

    // TODO - get the actual field name from the spec if it exists, else copy galeon
    m_visitcount_le->setReadOnly(true);
    m_visitcount_le->setText(
            NodeEditCommand::getNodeText(m_bk, TQStringList() << "info" << "metadata"
                                                           << "visit_count" ));
}

void BookmarkInfoWidget::commitChanges()
{
    commitTitle();
    commitURL();
    commitComment();
}

void BookmarkInfoWidget::commitTitle()
{
    if(titlecmd)
    {
        emit updateListViewItem();
        CurrentMgr::self()->notifyManagers(CurrentMgr::bookmarkAt(titlecmd->affectedBookmarks()).toGroup());
        titlecmd = 0;
    }
}

void BookmarkInfoWidget::slotTextChangedTitle(const TQString &str) 
{
    if (m_bk.isNull() || !m_title_le->isModified())
        return;

    timer->start(1000, true);

    if(titlecmd)
    {
        NodeEditCommand::setNodeText(m_bk, TQStringList() << "title", str);
        titlecmd->modify(str);
    }
    else
    {
        titlecmd = new NodeEditCommand(m_bk.address(), str, "title");
        titlecmd->execute();
        CmdHistory::self()->addInFlightCommand(titlecmd);
    }
}

void BookmarkInfoWidget::commitURL()
{
    if(urlcmd)
    {
        emit updateListViewItem();
        CurrentMgr::self()->notifyManagers(CurrentMgr::bookmarkAt(urlcmd->affectedBookmarks()).toGroup());
        urlcmd = 0;
    }
}

void BookmarkInfoWidget::slotTextChangedURL(const TQString &str) {
    if (m_bk.isNull() || !m_url_le->isModified())
        return;

    timer->start(1000, true);

    if(urlcmd)
    {
        KURL u = KURL::fromPathOrURL(str);
        m_bk.internalElement().setAttribute("href", u.url(0, 106));
        urlcmd->modify("href", u.url(0, 106));
    }
    else
    {
        KURL u = KURL::fromPathOrURL(str);
        urlcmd = new EditCommand(m_bk.address(), EditCommand::Edition("href", u.url(0, 106)), i18n("URL"));
        urlcmd->execute();
        CmdHistory::self()->addInFlightCommand(urlcmd);
    }
}

void BookmarkInfoWidget::commitComment()
{
    if(commentcmd)
    {
        emit updateListViewItem();
        CurrentMgr::self()->notifyManagers( CurrentMgr::bookmarkAt( commentcmd->affectedBookmarks() ).toGroup());
        commentcmd = 0;
    }
}

void BookmarkInfoWidget::slotTextChangedComment(const TQString &str) {
    if (m_bk.isNull() || !m_comment_le->isModified())
        return;

    timer->start(1000, true);

    if(commentcmd)
    {
        NodeEditCommand::setNodeText(m_bk, TQStringList() << "desc", str);
        commentcmd->modify(str);
    }
    else
    {
        commentcmd = new NodeEditCommand(m_bk.address(), str, "desc");
        commentcmd->execute();
        CmdHistory::self()->addInFlightCommand(commentcmd);
    }
}

BookmarkInfoWidget::BookmarkInfoWidget(TQWidget *parent, const char *name)
    : TQWidget(parent, name), m_connected(false) {

    timer = new TQTimer(this);
    connect(timer, TQT_SIGNAL( timeout() ), TQT_SLOT( commitChanges()));

    titlecmd = 0;
    urlcmd = 0;
    commentcmd = 0;

    TQBoxLayout *vbox = new TQVBoxLayout(this);
    TQGridLayout *grid = new TQGridLayout(vbox, 3, 4, 4);

    m_title_le = new BookmarkLineEdit(this);
    grid->addWidget(m_title_le, 0, 1);
    grid->addWidget(
            new TQLabel(m_title_le, i18n("Name:"), this),
            0, 0);

    connect(m_title_le, TQT_SIGNAL( textChanged(const TQString &) ),
                        TQT_SLOT( slotTextChangedTitle(const TQString &) ));
    connect(m_title_le, TQT_SIGNAL( lostFocus() ), TQT_SLOT( commitTitle() ));

    m_url_le = new BookmarkLineEdit(this);
    grid->addWidget(m_url_le, 1, 1);
    grid->addWidget(
            new TQLabel(m_url_le, i18n("Location:"), this),
            1, 0);

    connect(m_url_le, TQT_SIGNAL( textChanged(const TQString &) ),
                      TQT_SLOT( slotTextChangedURL(const TQString &) ));
    connect(m_url_le, TQT_SIGNAL( lostFocus() ), TQT_SLOT( commitURL() ));

    m_comment_le = new BookmarkLineEdit(this);
    grid->addWidget(m_comment_le, 2, 1);
    grid->addWidget(
            new TQLabel(m_comment_le, i18n("Comment:"), this),
            2, 0);
    connect(m_comment_le, TQT_SIGNAL( textChanged(const TQString &) ),
                          TQT_SLOT( slotTextChangedComment(const TQString &) ));
    connect(m_comment_le, TQT_SIGNAL( lostFocus() ), TQT_SLOT( commitComment() ));

    m_credate_le = new KLineEdit(this);
    grid->addWidget(m_credate_le, 0, 3);
    grid->addWidget(
            new TQLabel(m_credate_le, i18n("First viewed:"), this),
            0, 2);

    m_visitdate_le = new KLineEdit(this);
    grid->addWidget(m_visitdate_le, 1, 3);
    grid->addWidget(
            new TQLabel(m_visitdate_le, i18n("Viewed last:"), this),
            1, 2 );

    m_visitcount_le = new KLineEdit(this);
    grid->addWidget(m_visitcount_le, 2, 3);
    grid->addWidget(
            new TQLabel(m_visitcount_le, i18n("Times visited:"), this),
            2, 2);
}

#include "bookmarkinfo.moc"