summaryrefslogtreecommitdiffstats
path: root/kspread/dialogs/link.cpp
blob: e0c1be2177dc9ba74d389133f3f27ee2fc08f0c3 (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
/* This file is part of the KDE project
   Copyright (C) 2005 Ariya Hidayat <ariya@kde.org>
             (C) 2003 Norbert Andres <nandres@web.de>
             (C) 2002 Philipp Mueller <philipp.mueller@gmx.de>
             (C) 1999-2002 Laurent Montel <montel@kde.org>
             (C) 1999 Stephan Kulow <coolo@kde.org>
             (C) 1998-1999 Torben Weis <weis@kde.org>
   
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
*/

#include "link.h"

#include <tqcombobox.h>
#include <tqframe.h>
#include <tqlabel.h>
#include <tqlayout.h>

#include <kdesktopfile.h>
#include <kdialogbase.h>
#include <kiconloader.h>
#include <klineedit.h>
#include <tdemessagebox.h>
#include <tdelocale.h>
#include <tderecentdocument.h>
#include <kurlrequester.h>

using namespace KSpread;

class LinkDialog::Private
{
public:
    TQString text;
    TQFrame* internetPage;
    KLineEdit* internetText;
    KLineEdit* internetLink;
    TQFrame* mailPage;
    KLineEdit* mailText;
    KLineEdit* mailLink;
    TQFrame* filePage;
    KLineEdit* fileText;
    KURLRequester* fileLink;
    TQFrame* cellPage;
    KLineEdit* cellText;
    KLineEdit* cellLink;
};

LinkDialog::LinkDialog( TQWidget*, const char* )
  :  KDialogBase( KDialogBase::IconList,i18n( "Insert Link") ,
                  KDialogBase::Ok | KDialogBase::Cancel,
                  KDialogBase::Ok )
{
    d = new Private;
    
    // link for web or ftp
    d->internetPage = addPage( i18n( "Internet" ), TQString(), 
        BarIcon( "text-html",TDEIcon::SizeMedium ) );
    TQVBoxLayout* iLayout = new TQVBoxLayout( d->internetPage, marginHint(), spacingHint() );
    iLayout->add( new TQLabel( i18n("Text to display:" ), d->internetPage ) );
    d->internetText = new KLineEdit( d->internetPage );
    iLayout->add( d->internetText );
    iLayout->add( new TQLabel( i18n("Internet address:" ), d->internetPage ) );
    d->internetLink = new KLineEdit( d->internetPage );
    iLayout->add( d->internetLink );
    iLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
    connect( d->internetText, TQT_SIGNAL( textChanged( const TQString& ) ), this,
        TQT_SLOT( setText( const TQString& ) ) );
    
    // link for e-mail
    d->mailPage = addPage( i18n( "Mail" ), TQString(), 
        BarIcon( "mail_generic",TDEIcon::SizeMedium ) );
    TQVBoxLayout* mLayout = new TQVBoxLayout( d->mailPage, marginHint(), spacingHint() );
    mLayout->add( new TQLabel( i18n("Text to display:" ), d->mailPage ) );
    d->mailText = new KLineEdit( d->mailPage );
    mLayout->add( d->mailText );
    mLayout->add( new TQLabel( i18n("Email:" ), d->mailPage ) );
    d->mailLink = new KLineEdit( d->mailPage );
    mLayout->add( d->mailLink );
    mLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
    connect( d->mailText, TQT_SIGNAL( textChanged( const TQString& ) ), this,
        TQT_SLOT( setText( const TQString& ) ) );
    
    // link for external file
    d->filePage = addPage( i18n( "File" ), TQString(), 
        BarIcon( "document-new",TDEIcon::SizeMedium ) );
    TQVBoxLayout* fLayout = new TQVBoxLayout( d->filePage, marginHint(), spacingHint() );
    fLayout->add( new TQLabel( i18n("Text to display:" ), d->filePage ) );
    d->fileText = new KLineEdit( d->filePage );
    fLayout->add( d->fileText );
    fLayout->add( new TQLabel( i18n("File location:" ), d->filePage ) );
    d->fileLink = new KURLRequester( d->filePage );
    fLayout->add( d->fileLink );
    fLayout->add( new TQLabel( i18n("Recent file:" ), d->filePage ) );
    TQComboBox* recentFile = new TQComboBox( d->filePage );
    recentFile->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed );
    fLayout->add( recentFile );
    fLayout->addItem( new TQSpacerItem( 0, 40, TQSizePolicy::Minimum, TQSizePolicy::MinimumExpanding ) );
    connect( d->fileText, TQT_SIGNAL( textChanged( const TQString& ) ), this,
        TQT_SLOT( setText( const TQString& ) ) );
    TQObject::connect( recentFile, TQT_SIGNAL( highlighted ( const TQString &) ),
        d->fileLink->lineEdit(), TQT_SLOT( setText( const TQString & ) ) );
    
    
    // populate recent files
    TQStringList fileList = TDERecentDocument::recentDocuments();
    for( TQStringList::ConstIterator it = fileList.begin();it != fileList.end(); ++it )
    {
        KDesktopFile f(*it, true /* read only */);
        if ( !f.readURL().isEmpty() )
            recentFile->insertItem( f.readURL() );
    }
    if( recentFile->count()==0 )
    {
        recentFile->insertItem( i18n("No Entries") );
        recentFile->setEnabled( false );
    }    
    
    // link to another cell
    d->cellPage =  addPage( i18n( "Cell" ), TQString(), 
        BarIcon( "misc",TDEIcon::SizeMedium ) );
    TQVBoxLayout* cLayout = new TQVBoxLayout( d->cellPage, marginHint(), spacingHint() );
    cLayout->add( new TQLabel( i18n("Text to display:" ), d->cellPage ) );
    d->cellText = new KLineEdit( d->cellPage );
    cLayout->add( d->cellText );
    cLayout->add( new TQLabel( i18n("Cell:" ), d->cellPage ) );
    d->cellLink = new KLineEdit( d->cellPage );
    cLayout->add( d->cellLink );
    cLayout->addItem( new TQSpacerItem( 0, 0, TQSizePolicy::Minimum, TQSizePolicy::Expanding ) );
    connect( d->cellText, TQT_SIGNAL( textChanged( const TQString& ) ), this,
        TQT_SLOT( setText( const TQString& ) ) );
    
    enableButtonSeparator( true );
    d->internetText->setFocus();
    resize( 400,300 );
}

LinkDialog::~LinkDialog()
{
    delete d;
}

TQString LinkDialog::text() const
{
    return d->text;
}

TQString LinkDialog::link() const
{
    TQString str;
    switch( activePageIndex() )
    {
        case 0:  
          str = d->internetLink->text();  
          if( !str.isEmpty() )
          if( str.find( "http://" )==-1 )
          if( str.find( "https://" )==-1 )
          if( str.find( "ftp://" )==-1 )
              str.prepend( "http://" );
          break;
          
        case 1:  
           str = d->mailLink->text(); 
           if( !str.isEmpty() )
           if( str.find( "mailto:" )==-1 )
               str.prepend( "mailto:" );
           break;
           
        case 2:  
           str = d->fileLink->lineEdit()->text(); 
           if( !str.isEmpty() )
           if( str.find( "file:/" )==-1 )
               str.prepend( "file://" );
           break;
           
        case 3:  
            str = d->cellLink->text(); 
            break;
            
        break;
    }
    return str;
}

void LinkDialog::setText( const TQString& text )
{
    d->text = text;
    
    d->internetText->blockSignals( true );
    d->internetText->setText( text );
    d->internetText->blockSignals( false );
    
    d->mailText->blockSignals( true );
    d->mailText->setText( text );
    d->mailText->blockSignals( false );

    d->fileText->blockSignals( true );
    d->fileText->setText( text );
    d->fileText->blockSignals( false );
    
    d->cellText->blockSignals( true );
    d->cellText->setText( text );
    d->cellText->blockSignals( false );
}

// link must be complete, e.g. "http://www.koffice.org" instead of 
// "www.koffice.org" only, since protocol is used to decide which page to show
void LinkDialog::setLink( const TQString& link )
{
    if( link.startsWith( "https://" ) )
    {
      d->internetLink->setText( link.mid( TQString("https://").length() ) );
      showPage( 0 );
      return;
    }
    
    if( link.startsWith( "http://" ) )
    {
      d->internetLink->setText( link.mid( TQString("http://").length() ) );
      showPage( 0 );
      return;
    }
    
    if( link.startsWith( "ftp://" ) )
    {
      d->internetLink->setText( link.mid( TQString("ftp://").length() ) );
      showPage( 0 );
      return;
    }
    
    if( link.startsWith( "mailto:" ) )
    {
      d->mailLink->setText( link.mid( TQString("mailto:").length() ) );
      showPage( 1 );
      return;
    }
    
    if( link.startsWith( "file:/" ) )
    {
      TQString s = link.mid( TQString("file:/").length() );
      while(s.startsWith("//")) s.remove(0,1);
      d->fileLink->lineEdit()->setText(s);
      showPage( 2 );
      return;
    }
    
    // assume cell reference
    d->cellLink->setText( link );
    showPage( 3 );
}

void LinkDialog::slotOk()
{
    TQString str;
    switch( activePageIndex() )
    {
        case 0:  str = i18n( "Internet address is empty" );  break;
        case 1:  str = i18n( "Mail address is empty" ); break;
        case 2:  str = i18n( "File name is empty" ); break;
        case 3:  str = i18n( "Destination cell is empty" ); break;
        break;
    }
    
    if( link().isEmpty() )
    {
       KMessageBox::error( this, str );
       return;
    } 
    
    if( d->text.isEmpty() )
        d->text = link();
    
    accept();
}

#include "link.moc"