summaryrefslogtreecommitdiffstats
path: root/kfile-plugins/deb/kfile_deb.cpp
blob: b66e06296addba81f835d21eefcc14a0f1ba1d85 (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
/* This file is part of the KDE project
 * Copyright (C) 2002 Laurence Anderson <l.d.anderson@warwick.ac.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 <kprocess.h>
#include <klocale.h>
#include <kgenericfactory.h>
#include <kstringvalidator.h>
#include <kdebug.h>

#include <qdict.h>
#include <qvalidator.h>
#include <qcstring.h>
#include <qfile.h>
#include <qdatetime.h>
#include <qbuffer.h>
#include <kfilterdev.h>
#include <qregexp.h>
#include <karchive.h>
#include <ktar.h>
#include <kar.h>

#include "kfile_deb.h"

typedef KGenericFactory<KDebPlugin> DebFactory;

K_EXPORT_COMPONENT_FACTORY(kfile_deb, DebFactory( "kfile_deb" ))

KDebPlugin::KDebPlugin(QObject *parent, const char *name,
                       const QStringList &args)
    
    : KFilePlugin(parent, name, args)
{
    KFileMimeTypeInfo* info = addMimeTypeInfo( "application/x-deb" );
    KFileMimeTypeInfo::GroupInfo* group = 0L;
    group = addGroupInfo(info, "General", i18n("General"));
    KFileMimeTypeInfo::ItemInfo* item;

    item = addItemInfo(group, "Name", i18n("Name"), QVariant::String);
    item = addItemInfo(group, "Version", i18n("Version"), QVariant::String);
    item = addItemInfo(group, "Summary", i18n("Summary"), QVariant::String);
    item = addItemInfo(group, "Size", i18n("Size"), QVariant::Int);
}

bool KDebPlugin::readInfo( KFileMetaInfo& info, uint /*what*/)
{
    KAr debfile (info.path());
  
    if ( !debfile.open( IO_ReadOnly ) ) {
        kdDebug(7034) << "Couldn't open " << QFile::encodeName(info.path()) << endl;
        return false;    
    }

    const KArchiveDirectory* debdir = debfile.directory();
    const KArchiveEntry* controlentry = debdir->entry( "control.tar.gz" );
    if ( !controlentry || !controlentry->isFile() ) {
        kdWarning(7034) << "control.tar.gz not found" << endl;
        return false;
    }

    QIODevice* filterDev = KFilterDev::device( static_cast<const KArchiveFile *>( controlentry )->device(), "application/x-gzip" );
    if ( !filterDev ) {
        kdWarning(7034) << "Couldn't create filter device for control.tar.gz" << endl;
        return false;
    }
    KTar tarfile( filterDev );

    if ( !tarfile.open( IO_ReadOnly ) ) {
        kdWarning(7034) << "Couldn't open control.tar.gz" << endl;
        return false;
    }

    const KArchiveDirectory* controldir = tarfile.directory();
    Q_ASSERT( controldir );
    
    const KArchiveEntry* controlfile = controldir->entry( "control" );
    
    Q_ASSERT( controlfile );
    if (controlfile) {
        KFileMetaInfoGroup group = appendGroup(info, "General");	
        QByteArray control( static_cast<const KArchiveFile *>(controlfile)->data() );
    
        // Now process control array
        QBuffer controldev(control);
        controldev.open( IO_ReadOnly );
        while (!controldev.atEnd()) {
            char linebuf[100];
            controldev.readLine(linebuf, sizeof( linebuf ));
            QString line(linebuf);
            int fieldstart = line.find(QRegExp(":"), 0) + 2;
            if (fieldstart == 1) break;
            QString fieldname = line.mid(0, fieldstart - 2);
            QString fielddata = line.mid(fieldstart, line.length() - fieldstart - 1);

            if (fieldname == "Package") appendItem(group, "Name", fielddata);
            else if (fieldname == "Version") appendItem(group, "Version", fielddata);
            else if (fieldname == "Description") appendItem(group, "Summary", fielddata);
            else if (fieldname == "Installed-Size") appendItem(group, "Size", int(fielddata.toInt()));

            kdDebug(7034) << "Name: [" << fieldname << "] Data: [" << fielddata << "]" << endl;
        }
    } else {
        kdDebug(7034) << "Couldn't read control file" << endl;
        return false;
    }
    
    tarfile.close();    
    debfile.close();

    return true;
}

#include "kfile_deb.moc"