summaryrefslogtreecommitdiffstats
path: root/tdefile-plugins/xbm/tdefile_xbm.cpp
blob: d4871d6f5e1cc0bea94bc9925ce2b3bfcd6e7d55 (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
/* This file is part of the KDE project
 * Copyright (C) 2002 Shane Wright <me@shanewright.co.uk>
 *
 * 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 version 2.
 *
 * 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 <config.h>
#include "tdefile_xbm.h"

#include <kprocess.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <kstringvalidator.h>
#include <kdebug.h>

#include <tqdict.h>
#include <tqvalidator.h>
#include <tqcstring.h>
#include <tqfile.h>
#include <tqdatetime.h>

#if !defined(__osf__)
#include <inttypes.h>
#else
typedef unsigned short uint32_t;
#endif

typedef KGenericFactory<KXbmPlugin> XbmFactory;

K_EXPORT_COMPONENT_FACTORY(tdefile_xbm, XbmFactory( "tdefile_xbm" ))

KXbmPlugin::KXbmPlugin(TQObject *parent, const char *name,
                       const TQStringList &args)

    : KFilePlugin(parent, name, args)
{
    KFileMimeTypeInfo* info = addMimeTypeInfo( "image/x-xbm" );

    KFileMimeTypeInfo::GroupInfo* group = 0L;

    group = addGroupInfo(info, "Technical", i18n("Technical Details"));

    KFileMimeTypeInfo::ItemInfo* item;

    item = addItemInfo(group, "Dimensions", i18n("Dimensions"), TQVariant::Size);
    setHint( item, KFileMimeTypeInfo::Size );
    setUnit(item, KFileMimeTypeInfo::Pixels);
}


unsigned long KXbmPlugin::xbm_processLine(char * linebuf)
{
    const char * fsig = "#define ";

    // check it starts with #define
    if (memcmp(linebuf, fsig, 8))
        return 0;

    // scan for the 2nd space and set up a pointer
    uint32_t slen = strlen(linebuf);
    bool done = false;
    uint32_t spos = 0;
    unsigned char spacecount = 0;
    do {

        if (linebuf[spos] == 0x00)
            return 0;

        if (linebuf[spos] == ' ')
            ++spacecount;

        if (spacecount == 2)
            done = true;
        else
            ++spos;

    } while (!done);

    return atoi(linebuf + spos);
}


bool KXbmPlugin::readInfo( KFileMetaInfo& info, uint what)
{

    TQFile file(info.path());

    if (!file.open(IO_ReadOnly))
    {
        kdDebug(7034) << "Couldn't open " << TQFile::encodeName(info.path()).data() << endl;
        return false;
    }

    // we need a buffer for lines
    char linebuf[1000];

    // read the first line
    file.readLine(linebuf, sizeof( linebuf ));
    uint32_t width = xbm_processLine(linebuf);

    // read the 2nd line
    file.readLine(linebuf, sizeof( linebuf ));
    uint32_t height = xbm_processLine(linebuf);

    if ((width > 0) && (height > 0)) {
        // we have valid looking data
        KFileMetaInfoGroup group = appendGroup(info, "Technical");
        appendItem(group, "Dimensions", TQSize(width, height));
        return true;
    }

    return false;
}

#include "tdefile_xbm.moc"