KoInsertLink.cpp
1/* This file is part of the KDE project
2 Copyright (C) 2001 Montel Laurent <lmontel@mandrakesoft.com>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
18*/
19
20#include <tdeapplication.h>
21#include <tdelocale.h>
22
23#include <tqlayout.h>
24#include <tqvbox.h>
25#include <kdebug.h>
26#include <tqlabel.h>
27#include <tqcombobox.h>
28
29#include <klineedit.h>
30#include <kurlrequester.h>
31#include <kseparator.h>
32#include <kiconloader.h>
33#include "KoInsertLink.h"
34#include <kdesktopfile.h>
35#include <tderecentdocument.h>
36
37using namespace KOfficePrivate;
38
39KoInsertLinkDia::KoInsertLinkDia( TQWidget *parent, const char *name, bool displayBookmarkLink )
40 : KDialogBase( KDialogBase::IconList, i18n("Insert Link"),
41 KDialogBase::Ok | KDialogBase::Cancel,
42 KDialogBase::Ok, parent, name )
43{
44 bookmarkLink = 0L;
45 TQVBox *page=addVBoxPage(i18n("Internet"), TQString(),BarIcon("text-html",TDEIcon::SizeMedium));
46 internetLink = new internetLinkPage(page );
47 connect(internetLink,TQ_SIGNAL(textChanged()),this,TQ_SLOT(slotTextChanged ( )));
48
49 page=addVBoxPage(i18n("Mail & News"), TQString(),BarIcon("mail_generic",TDEIcon::SizeMedium));
50 mailLink = new mailLinkPage(page );
51 connect(mailLink,TQ_SIGNAL(textChanged()),this,TQ_SLOT(slotTextChanged ()));
52
53 page=addVBoxPage(i18n("File"), TQString(),BarIcon("document-new",TDEIcon::SizeMedium));
54 fileLink = new fileLinkPage(page );
55 connect(fileLink,TQ_SIGNAL(textChanged()),this,TQ_SLOT(slotTextChanged ()));
56
57 if ( displayBookmarkLink)
58 {
59 page=addVBoxPage(i18n("Bookmark"), TQString(),BarIcon("bookmark",TDEIcon::SizeMedium));
60 bookmarkLink = new bookmarkLinkPage(page );
61 connect(bookmarkLink,TQ_SIGNAL(textChanged()),this,TQ_SLOT(slotTextChanged ()));
62 }
63
64 connect( this, TQ_SIGNAL( aboutToShowPage(TQWidget *) ), this, TQ_SLOT( tabChanged(TQWidget *) ) );
65
66 slotTextChanged ( );
67 resize(400,300);
68}
69
70void KoInsertLinkDia::tabChanged(TQWidget *)
71{
72 switch( activePageIndex() )
73 {
74 case 0:
75 internetLink->setLinkName( currentText );
76 break;
77 case 1:
78 mailLink->setLinkName( currentText );
79 break;
80 case 2:
81 fileLink->setLinkName( currentText );
82 break;
83 case 3:
84 {
85 if ( bookmarkLink)
86 bookmarkLink->setLinkName( currentText );
87 }
88 break;
89 default:
90 kdDebug()<<"Error in linkName\n";
91 }
92 enableButtonOK( !(linkName().isEmpty() || hrefName().isEmpty()) );
93}
94
95void KoInsertLinkDia::slotTextChanged ( )
96{
97 enableButtonOK( !(linkName().isEmpty() || hrefName().isEmpty()));
98 currentText = linkName();
99}
100
101bool KoInsertLinkDia::createLinkDia(TQString & _linkName, TQString & _hrefName, const TQStringList& bkmlist, bool displayBookmarkLink, TQWidget* parent, const char* name)
102{
103 bool res = false;
104
105 KoInsertLinkDia *dlg = new KoInsertLinkDia( parent, name, displayBookmarkLink );
106 dlg->setHrefLinkName(_hrefName,_linkName, bkmlist);
107 if ( dlg->exec() == Accepted )
108 {
109 _linkName = dlg->linkName();
110 _hrefName = dlg->hrefName();
111 res = true;
112 }
113 delete dlg;
114
115 return res;
116}
117
118void KoInsertLinkDia::setHrefLinkName(const TQString &_href, const TQString &_link, const TQStringList & bkmlist)
119{
120 if ( bookmarkLink)
121 bookmarkLink->setBookmarkList(bkmlist);
122 if ( _href.isEmpty())
123 {
124 if ( !_link.isEmpty() )
125 {
126 internetLink->setLinkName(_link);
127 showPage(0);
128 slotTextChanged ( );
129 }
130 return;
131 }
132 if(_href.find("http://")!=-1 || _href.find("https://")!=-1 ||_href.find("ftp://")!=-1 )
133 {
134 internetLink->setHrefName(_href);
135 internetLink->setLinkName(_link);
136 showPage(0);
137 }
138 else if(_href.find("file:/")!=-1)
139 {
140 fileLink->setHrefName(_href);
141 fileLink->setLinkName(_link);
142 showPage(2);
143 }
144 else if(_href.find("mailto:")!=-1 || _href.find("news:")!=-1)
145 {
146 mailLink->setHrefName(_href);
147 mailLink->setLinkName(_link);
148 showPage(1);
149 }
150 else if(_href.find("bkm://")!=-1)
151 {
152 if ( bookmarkLink )
153 {
154 bookmarkLink->setHrefName(_href.mid(6));
155 bookmarkLink->setLinkName(_link);
156 showPage(3);
157 }
158 }
159 slotTextChanged ( );
160}
161
162TQString KoInsertLinkDia::linkName() const
163{
164 TQString result;
165 switch(activePageIndex())
166 {
167 case 0:
168 result=internetLink->linkName();
169 break;
170 case 1:
171 result=mailLink->linkName();
172 break;
173 case 2:
174 result=fileLink->linkName();
175 break;
176 case 3:
177 {
178 if ( bookmarkLink)
179 result=bookmarkLink->linkName();
180 }
181 break;
182 default:
183 kdDebug()<<"Error in linkName\n";
184 }
185 return result;
186}
187
188TQString KoInsertLinkDia::hrefName() const
189{
190 TQString result;
191 switch(activePageIndex())
192 {
193 case 0:
194 result=internetLink->hrefName();
195 break;
196 case 1:
197 result=mailLink->hrefName();
198 break;
199 case 2:
200 result=fileLink->hrefName();
201 break;
202 case 3:
203 {
204 if ( bookmarkLink )
205 result=bookmarkLink->hrefName();
206 }
207 break;
208 default:
209 kdDebug()<<"Error in hrefName\n";
210 }
211 return result;
212}
213
214void KoInsertLinkDia::slotOk()
215{
217}
218
219
220internetLinkPage::internetLinkPage( TQWidget *parent , char *name )
221 : TQWidget(parent,name)
222{
223 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
224 lay1->setSpacing( KDialog::spacingHint() );
225 TQVBoxLayout *lay2 = new TQVBoxLayout( lay1);
226 lay2->setSpacing( KDialog::spacingHint() );
227
228 TQLabel* tmpTQLabel = new TQLabel( this);
229
230 lay2->addWidget(tmpTQLabel);
231 tmpTQLabel->setText(i18n("Text to display:"));
232
233 m_linkName = new TQLineEdit( this );
234 lay2->addWidget(m_linkName);
235
236 tmpTQLabel = new TQLabel( this);
237 lay2->addWidget(tmpTQLabel);
238
239 tmpTQLabel->setText(i18n("Internet address:"));
240 m_hrefName = new TQLineEdit( this );
241
242 lay2->addWidget(m_hrefName);
243
244 lay2->addStretch( 1 );
245
246 m_linkName->setFocus();
247
248 connect(m_linkName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
249 connect(m_hrefName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
250 KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
251 bar1->setFixedHeight( 10 );
252 lay2->addWidget( bar1 );
253}
254
255TQString internetLinkPage::createInternetLink()
256{
257 TQString result=m_hrefName->text();
258
259 if(result.isEmpty())
260 return result;
261
262 if(result.find("http://")==-1 && result.find("https://")==-1 && result.find("ftp://")==-1)
263 result = "http://"+result;
264 return result;
265}
266
267
268void internetLinkPage::setLinkName(const TQString & _name)
269{
270 m_linkName->setText(_name);
271}
272
273void internetLinkPage::setHrefName(const TQString &_name)
274{
275 m_hrefName->setText(_name);
276}
277
278TQString internetLinkPage::linkName()const
279{
280 return m_linkName->text();
281}
282
283TQString internetLinkPage::hrefName()
284{
285 return createInternetLink();
286}
287
288void internetLinkPage::textChanged ( const TQString & )
289{
290 emit textChanged();
291}
292
293bookmarkLinkPage::bookmarkLinkPage( TQWidget *parent , char *name )
294 : TQWidget(parent,name)
295{
296 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
297 lay1->setSpacing( KDialog::spacingHint() );
298 TQVBoxLayout *lay2 = new TQVBoxLayout( lay1);
299 lay2->setSpacing( KDialog::spacingHint() );
300
301 TQLabel* tmpTQLabel = new TQLabel( this);
302
303 lay2->addWidget(tmpTQLabel);
304 tmpTQLabel->setText(i18n("Text to display:"));
305
306 m_linkName = new TQLineEdit( this );
307 lay2->addWidget(m_linkName);
308
309 tmpTQLabel = new TQLabel( this);
310 lay2->addWidget(tmpTQLabel);
311
312 tmpTQLabel->setText(i18n("Bookmark name:"));
313 m_hrefName = new TQComboBox( this );
314
315 lay2->addWidget(m_hrefName);
316
317 lay2->addStretch( 1 );
318
319 m_linkName->setFocus();
320
321 connect(m_linkName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
322 connect(m_hrefName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
323 KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
324 bar1->setFixedHeight( 10 );
325 lay2->addWidget( bar1 );
326}
327
328TQString bookmarkLinkPage::createBookmarkLink()
329{
330 TQString result=m_hrefName->currentText();
331
332 if(result.isEmpty())
333 return result;
334
335 if(result.find("bkm://")==-1)
336 result = "bkm://"+result;
337 return result;
338}
339
340
341void bookmarkLinkPage::setLinkName(const TQString & _name)
342{
343 m_linkName->setText(_name);
344}
345
346void bookmarkLinkPage::setHrefName(const TQString &_name)
347{
348 m_hrefName->setCurrentText(_name);
349}
350
351void bookmarkLinkPage::setBookmarkList(const TQStringList & bkmlist)
352{
353 m_hrefName->clear();
354 m_hrefName->insertStringList(bkmlist, 0);
355 if ( bkmlist.isEmpty())
356 m_linkName->setEnabled( false);
357 //m_hrefName->setEditable(true);
358}
359
360TQString bookmarkLinkPage::linkName()const
361{
362 return m_linkName->text();
363}
364
365TQString bookmarkLinkPage::hrefName()
366{
367 return createBookmarkLink();
368}
369
370void bookmarkLinkPage::textChanged ( const TQString & )
371{
372 emit textChanged();
373}
374
375mailLinkPage::mailLinkPage( TQWidget *parent , char *name )
376 : TQWidget(parent,name)
377{
378 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
379 lay1->setSpacing( KDialog::spacingHint() );
380 TQVBoxLayout *lay2 = new TQVBoxLayout( lay1);
381 lay2->setSpacing( KDialog::spacingHint() );
382
383 TQLabel* tmpTQLabel = new TQLabel( this);
384
385 lay2->addWidget(tmpTQLabel);
386 tmpTQLabel->setText(i18n("Text to display:"));
387
388 m_linkName = new TQLineEdit( this );
389 lay2->addWidget(m_linkName);
390
391 tmpTQLabel = new TQLabel( this);
392 lay2->addWidget(tmpTQLabel);
393
394 tmpTQLabel->setText(i18n("Target:"));
395 m_hrefName = new TQLineEdit( this );
396
397 lay2->addWidget(m_hrefName);
398 lay2->addStretch( 1 );
399
400 connect(m_linkName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
401 connect(m_hrefName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
402 KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
403 bar1->setFixedHeight( 10 );
404 lay2->addWidget( bar1 );
405}
406
407TQString mailLinkPage::createMailLink()
408{
409 TQString result=m_hrefName->text();
410
411 if(result.isEmpty())
412 return result;
413
414 if(result.find("mailto:")==-1 && result.find("news:")==-1)
415 result = "mailto:"+result;
416 return result;
417}
418
419
420void mailLinkPage::setLinkName(const TQString & _name)
421{
422 m_linkName->setText(_name);
423}
424
425void mailLinkPage::setHrefName(const TQString &_name)
426{
427 m_hrefName->setText(_name);
428}
429
430TQString mailLinkPage::linkName()const
431{
432 return m_linkName->text();
433}
434
435TQString mailLinkPage::hrefName()
436{
437 return createMailLink();
438}
439
440void mailLinkPage::textChanged ( const TQString & )
441{
442 emit textChanged();
443}
444
445fileLinkPage::fileLinkPage( TQWidget *parent , char *name )
446 : TQWidget(parent,name)
447{
448 TQVBoxLayout *lay1 = new TQVBoxLayout( this );
449 lay1->setSpacing( KDialog::spacingHint() );
450 TQVBoxLayout *lay2 = new TQVBoxLayout( lay1);
451 lay2->setSpacing( KDialog::spacingHint() );
452
453 TQLabel* tmpTQLabel = new TQLabel( this);
454
455 lay2->addWidget(tmpTQLabel);
456 tmpTQLabel->setText(i18n("Text to display:"));
457
458 m_linkName = new TQLineEdit( this );
459 lay2->addWidget(m_linkName);
460
461 tmpTQLabel = new TQLabel( this);
462 lay2->addWidget(tmpTQLabel);
463 tmpTQLabel->setText(i18n("Recent file:"));
464
465 TQComboBox * recentFile = new TQComboBox( this );
466 recentFile->setMaximumWidth( kapp->desktop()->width()*3/4 );
467 lay2->addWidget(recentFile);
468
469 TQStringList fileList = TDERecentDocument::recentDocuments();
470 TQStringList lst;
471 lst <<"";
472 for (TQStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it)
473 {
474 KDesktopFile f(*it, true /* read only */);
475 if ( !f.readURL().isEmpty())
476 lst.append( f.readURL());
477 }
478 if ( lst.count()<= 1 )
479 {
480 recentFile->clear();
481 recentFile->insertItem( i18n("No Entries") );
482 recentFile->setEnabled( false );
483 }
484 else
485 recentFile->insertStringList( lst);
486
487 recentFile->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
488
489 connect( recentFile , TQ_SIGNAL(highlighted ( const TQString &)), this, TQ_SLOT( slotSelectRecentFile( const TQString & )));
490
491 tmpTQLabel = new TQLabel( this);
492 lay2->addWidget(tmpTQLabel);
493
494 tmpTQLabel->setText(i18n("File location:"));
495 m_hrefName = new KURLRequester( this );
496
497 lay2->addWidget(m_hrefName);
498 lay2->addStretch( 1 );
499
500 connect(m_linkName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
501 connect(m_hrefName,TQ_SIGNAL(textChanged ( const TQString & )),this,TQ_SLOT(textChanged ( const TQString & )));
502
503 KSeparator* bar1 = new KSeparator( KSeparator::HLine, this);
504 bar1->setFixedHeight( 10 );
505 lay2->addWidget( bar1 );
506}
507
508void fileLinkPage::slotSelectRecentFile( const TQString &_file )
509{
510 m_hrefName->lineEdit()->setText(_file );
511}
512
513TQString fileLinkPage::createFileLink()
514{
515 TQString result=m_hrefName->lineEdit()->text();
516 if(result.isEmpty())
517 return result;
518
519 if(result.find("file:/")==-1)
520 result = "file://"+result;
521 return result;
522}
523
524void fileLinkPage::setLinkName(const TQString & _name)
525{
526 m_linkName->setText(_name);
527}
528
529void fileLinkPage::setHrefName(const TQString &_name)
530{
531 m_hrefName->lineEdit()->setText(_name);
532}
533
534TQString fileLinkPage::linkName()const
535{
536 return m_linkName->text();
537}
538
539TQString fileLinkPage::hrefName()
540{
541 return createFileLink();
542}
543
544void fileLinkPage::textChanged ( const TQString & )
545{
546 emit textChanged();
547}
548
549#include "KoInsertLink.moc"
bool showPage(int index)
void enableButtonOK(bool state)
int activePageIndex() const
virtual void slotOk()
static int spacingHint()
Dialog to insert links to various sources (file, Internet, mail and bookmarks).
const char * name(StdAction id)