summaryrefslogtreecommitdiffstats
path: root/kiconedit/utils.cpp
blob: 1af528033e1e7252c912af31ecf3f8a72423ef79 (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
/*
    KDE Icon Editor - a small graphics drawing program for the KDE

    Copyright (C) 1998 Thomas Tanghus (tanghus@kde.org)

    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 <kimageio.h>
#include <klocale.h>
#include <kdebug.h>

#include "utils.h"
#include "config.h"

imageFormats *formats = 0L;

void setupImageHandlers()
{
  if(formats != 0L)
    return;

  KImageIO::registerFormats();

  kdDebug(4640) << "Initializing formats" << endl;
  formats = new imageFormats;
  Q_CHECK_PTR(formats);
  formats->setAutoDelete(true);
  formats->append(new imageFormat("GIF", "GIF",  "gif"));
#ifdef HAVE_LIBJPEG
  formats->append(new imageFormat("JFIF", "JPEG", "jpg"));
#endif
  formats->append(new imageFormat("XPM", "XPM",  "xpm"));
  formats->append(new imageFormat("ICO", "Windows Icon File",  "ico"));
/*
#ifdef HAVE_LIBJPEG
  QImageIO::defineIOHandler("JFIF","^\377\330\377\340", 0, read_jpeg_jfif, NULL);
#endif
*/
}

// Simple copy operation on local files (isn't there something like this in the libs?)
bool copyFile(const QString &src, const QString &dest)
{
  QFile f_src(src);
  QFile f_dest(dest);
  QFileInfo fi(f_src);
  uint src_size = fi.size();
  kdDebug(4640) << "Size: " << src_size << endl;

  if ( f_src.open(IO_ReadOnly) )
  {    // file opened successfully
    if ( !f_dest.open(IO_WriteOnly) )
    {
      kdDebug(4640) << "copyFile - There was an error opening destination file: " << dest << endl;
      f_src.close();
      return false;
    }
    char *data = new char[src_size];
    if(f_src.readBlock(data, src_size) == -1)
    {
      kdDebug(4640) << "copyFile - There was an error reading source file: " << src << endl;
      f_src.close();
      f_dest.close();
      delete [] data;
      return false;
    }
    if(f_dest.writeBlock(data, src_size) == -1)
    {
      kdDebug(4640) << "copyFile - There was an error writing to destination file: " << dest << endl;
      f_src.close();
      f_dest.close();
      delete [] data;
      return false;
    }

    f_src.close();
    f_dest.close();
    delete [] data;
    return true;
  }
  kdDebug(4640) << "copyFile - There was an error opening source file: " << src << endl;
  return false;
}

bool removeFile(const QString &file)
{
  if(file.length() > 0 && QFile::exists(file))
  {
    QDir d;
    kdDebug(4640) << "Removing " << file << endl;
    if(!d.remove(file))
    {
      kdDebug(4640) << "removeFile - There was an error removing the file: " << file << endl;
      return false;
    }
    return true;
  }
  return false;
}

bool moveFile(const QString &src, const QString &dest)
{
  if(copyFile(src, dest))
    return removeFile(src);
  return false;
}

uint kdeColor(uint color)
{
  uint c = iconpalette[0]|OPAQUE_MASK;

  for(uint i = 0; i < 42; i++)
  {
    //kdDebug(4640) << "Color #" << i << " " << iconpalette[i] << endl;
    if( (iconpalette[i]|OPAQUE_MASK) - c < (iconpalette[i]|OPAQUE_MASK) - color)
      c = iconpalette[i]|OPAQUE_MASK;
  }
  //kdDebug(4640) << color << " -> " << c << endl;
  return c;
}