summaryrefslogtreecommitdiffstats
path: root/kiconedit/kicon.cpp
blob: b16a78b5cdc5596b08e0682631bf25b3db179237 (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
/*
    TDE Icon Editor - a small graphics drawing program for the TDE
    Copyright (C) 1998  Thomas Tanghus ([email protected])

    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.

    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 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 <stdlib.h>

#include <tqimage.h>

#include <kmessagebox.h>
#include <kfiledialog.h>
#include <kio/netaccess.h>
#include <kimageio.h>
#include <kdebug.h>
#include <klocale.h>

#include "kicon.h"
#include "utils.h"

KIconEditIcon::KIconEditIcon(TQObject *parent, const TQImage *img, KURL url) 
   : TQObject(parent)
{
    f = 0;
    _lastdir = "/";
    //checkUnNamedBackup(img);

    if(!url.isEmpty())
    {
        open(img, url);
    }
}


KIconEditIcon::~KIconEditIcon()
{
}


bool KIconEditIcon::open(const TQImage *image, KURL url)
{
    TQImage *img = (TQImage*)image;

    if(url.isEmpty())
        return false;

    kdDebug(4640) << "KIconEditIcon::open " << url.prettyURL() << endl;
    
    if(!url.isValid()) // try to see if it is a relative filename
    {
        kdDebug(4640) << "KIconEditIcon::open (malformed) " << url.prettyURL() << endl;

        TQFileInfo fi(url.url());
        if(fi.isRelative())
            url = "file:" + fi.absFilePath();

        if(!url.isValid()) // Giving up
        {
            TQString msg = i18n("The URL: %1 \nseems to be malformed.\n").arg(url.url());
            KMessageBox::sorry((TQWidget*)parent(), msg);
            return false;
        }
    }

    TQString filename;

    if(url.isLocalFile())
    {
        filename = url.path();
    }
    else
    {
        if(!TDEIO::NetAccess::download( url, filename, (TQWidget*)parent() ))
        {
            TQString msg = i18n("There was an error loading:\n%1\n").arg(url.prettyURL());
            KMessageBox::error((TQWidget*)parent(), msg);
            return false;
        }
    }

    bool loadedOk = img->load(filename);

    if(!url.isLocalFile())
    {
        TDEIO::NetAccess::removeTempFile( filename );
    }

    if(!loadedOk) 
    {
          TQString msg = i18n("There was an error loading:\n%1\n").arg(url.prettyURL());
          KMessageBox::error((TQWidget*)parent(), msg);
    }
    else
    {
        kdDebug(4640) << "KIconEditIcon::open - Image loaded" << endl;
        
        // _url is saved off for use in saving the image to the same 
        // file later - should include full path
        if(url.isLocalFile())
        {
            _url = url.path();
        }
        else
        {
            _url = "";
        }
        
        kdDebug(4640) << "KIcon: _url: " << _url << endl;
        
        // this causes updates of preview, color palettes, etc.    
        emit loaded(img);
        kdDebug(4640) << "loaded(img)" <<  endl;    
        
        // this is the name that shows up in status bar - 
        // should be filename with path
        emit newname(url.prettyURL()); 
        kdDebug(4640) << "newname(_url) : " << url.prettyURL() << endl;    
        
        emit addrecent(url.prettyURL());
        
        kdDebug(4640) << "KIconEditIcon::open - done" << endl;
    }

    return loadedOk;
}



bool KIconEditIcon::promptForFile(const TQImage *img)
{
    kdDebug(4640) << "KIconEditIcon::promptForFile(const TQImage *img)" << endl;
    /*
    TQString filter = i18n("*|All Files (*)\n"
                        "*.xpm|XPM (*.xpm)\n"
                        "*.png|PNG (*.png)\n"
                        "*.gif|GIF files (*.gif)\n"
                        "*.jpg|JPEG files (*.jpg)\n"
                        "*.ico|Icon files (*.ico)\n");

  
    KURL url = KFileDialog::getOpenURL( TQString(), filter );
    */
    bool loaded = false;
    KURL url = KFileDialog::getImageOpenURL( TQString(), TQT_TQWIDGET(parent()) );

    if( !url.isEmpty() )
    {
        loaded = open( img, url );
    }

    return loaded;
}



bool KIconEditIcon::saveAs(const TQImage *image)
{
    kdDebug(4640) << "KIconEditIcon::saveAs" << endl;

    TQString file;

    //Get list of file types..
    KFileDialog *dialog=new KFileDialog(TQString(), TQString(), TQT_TQWIDGET(parent()), "file dialog", true);
    dialog->setCaption( i18n("Save Icon As") );
    dialog->setKeepLocation( true );
    dialog->setMimeFilter( KImageIO::mimeTypes(KImageIO::Writing), "image/png" );
    dialog->setOperationMode( KFileDialog::Saving );

    if(dialog->exec()==TQDialog::Accepted)
    {
        file = dialog->selectedFile();
        if( file.isNull() )
        {
            delete dialog;
            return false;
        }
        if ( !KImageIO::canWrite(KImageIO::type(file)) )
        {
            if ( KImageIO::canWrite(KImageIO::typeForMime(dialog->currentFilter())) )
                file += "."+KImageIO::suffix(KImageIO::typeForMime(dialog->currentFilter()));
            else
                file += ".png";
        }
    }
    else
    {
        delete dialog;
        return false;
    }
        
    delete dialog;

    if(TQFile::exists(file))
    {
        int r=KMessageBox::warningContinueCancel(TQT_TQWIDGET(parent()),
            i18n( "A file named \"%1\" already exists. "
                  "Overwrite it?" ).arg(file),
            i18n( "Overwrite File?" ),
            i18n( "&Overwrite" ) );
  
        if(r==KMessageBox::Cancel)
        {
            return false;
        }
    }

    return save( image, file );
}



bool KIconEditIcon::save(const TQImage *image, const TQString &_filename)
{
    kdDebug(4640) << "KIconEditIcon::save" << endl;
    TQString filename = _filename;

    if(filename.isEmpty())
    {
        if(_url.isEmpty())
        {
            return saveAs(image);
        }
        else
        {
            KURL turl(_url);
            filename = turl.path();
        }    
    }

    TQImage *img = (TQImage*)image;
    img->setAlphaBuffer(true);
    
    KURL turl(filename);
    TQString str = turl.path();
    bool savedOk = false;

    /* base image type on file extension - let kimageio
    take care of this determination */
    
    if(img->save(str, KImageIO::type(str).ascii()))    
    {
        kdDebug(4640) << "img->save()) successful" << endl;
        emit saved();    
        _url = filename;
        
        // emit signal to change title bar to reflect new name
        emit newname(filename); 
        kdDebug(4640) << "newname(filenamme) : " << _url << endl;    
        savedOk = true;
        emit addrecent(filename);
    }
    else 
    {
        TQString msg = i18n("There was an error saving:\n%1\n").arg(str);
        KMessageBox::error((TQWidget*)parent(), msg);
        kdDebug(4640) << "KIconEditIcon::save - " << msg << endl;
    }

    kdDebug(4640) << "KIconEditIcon::save - done" << endl;    

    return savedOk;
}


#include "kicon.moc"