This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Writing a file preview plugin

Tags: None
(comma "," separated)
User avatar
QwertyManiac
Registered Member
Posts
5
Karma
0
OS

Writing a file preview plugin

Thu Oct 15, 2009 4:46 am
I'm willing to write a file preview 'plug-in' (if it works that way) for previewing CBZ/CBR comic book files in Dolphin. I just do not know which part of kdegraphics I must be looking at - can I get some guidance on where to start exactly?

Also, if this is already done, do let me know. I'm using KDE 4.3.2 right now and the feature isn't available in it.

(Okular is installed, with all its associated libs - Evince on GNOME shows previews for those files, so why not KDE as well!)


Harsh J
Blog | Code | Twitter
QwertyManiac, proud to be a member of KDE forums since 2008-Oct.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
One does not appear to exist at this time. You may find these API docs here to be of help: http://api.kde.org/4.x-api/kdelibs-apid ... eator.html

Existing file preview plugins appear to be in kdegraphics/thumbnailers/


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
aditya_bhatt
Registered Member
Posts
14
Karma
0
OS

Re: Writing a file preview plugin

Sun Oct 18, 2009 8:56 am
OMG Im just starting development on the same comic book previews thingy!
I'm willing to implement this too, as it will be a great learning experience for me. I know Qt development well enough, just want to start writing for kde. I'm just learning how to use svn as of now.


User avatar
aditya_bhatt
Registered Member
Posts
14
Karma
0
OS

Re: Writing a file preview plugin

Sun Oct 18, 2009 11:24 am
Does anyone here know how to register the plugin created after doing the below stuff (i got it from the KDE api docs ) :

Compile your ThumbCreator as a module. The contents of CMakeLists.txt should look something like this, with "filetype" replaced by the type of file this plugin creates thumbnails for:
project(filetypethumbcreator)

find_package(KDE4 REQUIRED)
include (KDE4Defaults)
include(MacroOptionalAddSubdirectory)

set(filetypethumbnail_SRCS filetypethumbnail.cpp)


kde4_add_ui_files(filetypethumbnail_SRCS config.ui )

kde4_add_plugin(filetypethumbnail ${filetypethumbnail_SRCS})
target_link_libraries(filetypethumbnail ${KDE4_KIO_LIBS})

install(TARGETS filetypethumbnail DESTINATION ${PLUGIN_INSTALL_DIR})
install(FILES filetypethumbcreator.desktop DESTINATION ${SERVICES_INSTALL_DIR})

Create a file filetypethumbcreator.desktop with the following contents:
[Desktop Entry]
Encoding=UTF-8
Type=Service
Name=Name of the type of files your ThumbCreator supports
ServiceTypes=ThumbCreator
MimeType=application/x-somemimetype;
CacheThumbnail=true
X-KDE-Library=yourthumbcreator


User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

Re: Writing a file preview plugin

Mon Oct 19, 2009 5:14 am
You may need to run kbuildsycoca4 after installing it into $KDEDIR which is usually /usr. You may also need to restart Dolphin. Otherwise, can you please post the source for your file?


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
aditya_bhatt
Registered Member
Posts
14
Karma
0
OS

Re: Writing a file preview plugin

Mon Oct 19, 2009 9:28 am
Well, this is the code :

Code: Select all
// comicthumbnail.h

#ifndef COMICTHUMBNAIL_H
#define COMICTHUMBNAIL_H

#include <QObject>
#include <kio/thumbcreator.h>

class ComicThumbnailCreator : public QObject, public ThumbCreator
{
    Q_OBJECT
public:
    ComicThumbnailCreator();
    virtual ~ComicThumbnailCreator();
    virtual bool create(const QString &path, int w, int h, QImage &img);
    virtual Flags flags() const;
private:
    QImage* m_signet;
};

#endif




and the other file :

Code: Select all
// comicthumbnail.cpp

#include "comicthumbnail.h"

#include <kzip.h>   // for extracting .cbz files
#include <QDebug>
#include <QImage>
#include <QPainter>
#include <QStringList>
extern "C"
{
    KDE_EXPORT ThumbCreator *new_creator()
    {
        return new ComicThumbnailCreator;
    }
}


ComicThumbnailCreator::ComicThumbnailCreator()
{
    m_signet = new QImage( ":/images/defaultimage" );
}

ComicThumbnailCreator::~ComicThumbnailCreator()
{
    if ( m_signet != NULL )
    {
        delete m_signet;
        m_signet = 0;
    }
}

bool ComicThumbnailCreator::create ( const QString &path, int /*w*/, int /*h*/, QImage &img )
{
   qDebug()<<"Entered create...";
    bool bRet = false;
    KZip zip ( path );
    qDebug()<<"Now opening the zip file...";
    if ( zip.open( QIODevice::ReadOnly ) )
    {
        const KArchiveDirectory* dir = zip.directory();
        QStringList entry = dir->entries();
   qDebug()<<"The entries are : "<<entry.join(",");
   entry.sort();
   qDebug()<<"In sorted form, they are : "<<entry.join(",");
   
   const QString *first = new QString(entry.at(0));
   qDebug()<<"In the sorted form, the first entry is : "<<*first;
   //first = entry.at(0);
   
   qDebug()<<"Will check if first!=NULL...";
        if ( ( first != NULL ))// && ( first.isFile() ) )
        {
      qDebug()<<"discovered that first!=NULL ... OK, good to go...";
      qDebug()<<"Now Creating a KArchiveFile pointer-object called file.";
            const KArchiveFile* file = ( KArchiveFile* ) first;
       qDebug()<<"Created a KArchiveFile pointer-object called file... OK.";
           
       qDebug()<<"Now Creating  a QByteArray object called 'data' from file->data()";
       QByteArray data ( file->data() );
      
       qDebug()<<"Created a QByteArray object called 'data' from file->data().";
       qDebug()<<"Its value is : "<<data;
      
            if ( data.size() > 0 )
            {
                img.loadFromData ( data,"PNG" );
#ifndef KEEP_ALPHA_CHANNEL
                if ( !img.isNull() && img.hasAlphaChannel() )
                    img = img.convertToFormat ( QImage::Format_RGB32 );
#endif
#ifndef LOLOL
                //const KArchiveEntry* docSigEntry = dir->entry( "META-INF/documentsignatures.xml" );
                if ( m_signet != NULL && !img.isNull() )//&& docSigEntry != NULL )
                {
                    int x = img.width() - m_signet->width() - 4;
                    x = qMax( x, 0 );
                    int y = 6;
                    QPainter p( &img );
                    p.drawImage( x, y, *m_signet );
                }
#endif
                if ( !img.isNull() )
                    bRet = true;
            }
        }
    }
    zip.close();
    return bRet;
}

ThumbCreator::Flags ComicThumbnailCreator::flags() const
{
    return (Flags)(DrawFrame | BlendIcon);
}
 





Well, now the problem is at QByteArray data(file->data()).
It crashes the program. (I wrote a test program to draw the first image on a window.)

Even calling file->copyTo(QString("temp")) to extract the image to another file crashes it.

I'm just not seeing why.
The crash information given by the KDE Crash handler is this :

Application: The Hello World Program (tutorial1), signal: Segmentation fault
[KCrash Handler]
#5 0x0000000000000000 in ?? ()
#6 0x0000000000403b94 in ComicThumbnailCreator::create (this=0x7fffc0a59440, path=<value optimized out>, img=...) at /home/aditya/dev/Thumbtest/comicthumbnail.cpp:60
#7 0x0000000000402962 in main (argc=<value optimized out>, argv=<value optimized out>) at /home/aditya/dev/Thumbtest/main.cpp:51




I know that the problem is with file->data() because the debug output is this :

path = "def.cbz"
Will now enter the create function
Entered create...
Now opening the zip file...
The entries are : "001.png,002.png"
In sorted form, they are : "001.png,002.png"
In the sorted form, the first entry is : "001.png"
Will check if first!=NULL...
discovered that first!=NULL ... OK, good to go...
Now Creating a KArchiveFile pointer-object called file.
Created a KArchiveFile pointer-object called file... OK.
Now Creating a QByteArray object called 'data' from file->data()
KCrash: Application 'tutorial1' crashing...
sock_file=/home/aditya/.kde4/socket-aditya-arch/kdeinit4__0



Can you help me with this? I want to know how to extract data from the zip file without crashing the program. Thanks in advance :)


User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

Re: Writing a file preview plugin

Tue Oct 20, 2009 4:25 am
You can't cast QString to KArchiveFile.
You need to create KArchiveFile using its API, see api.kde.org/classmapper.php?class=KArchiveFile


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
aditya_bhatt
Registered Member
Posts
14
Karma
0
OS

Re: Writing a file preview plugin

Tue Oct 20, 2009 4:06 pm
Uh-well, I had figured that already :P... anyway its done now - you can mark this thread as closed!


User avatar
QwertyManiac
Registered Member
Posts
5
Karma
0
OS

Re: Writing a file preview plugin

Sat Oct 24, 2009 6:07 pm
bcooksley wrote:One does not appear to exist at this time. You may find these API docs here to be of help: http://api.kde.org/4.x-api/kdelibs-apid ... eator.html

Existing file preview plugins appear to be in kdegraphics/thumbnailers/


Hello again. Thank you for your help! :)

After looking at some more thumbnail plugins in KDE/kdebase/kioslave/thumbnail, I managed to write my own.

The plugin, whose description and file hosting can be found here (under GPLv3), supports ComicBook files of type .cbr, .cbz and .cbt (RAR, ZIP and TAR). I plan to add .cb7 and .cba (p7zip/ACE) support soon.

Current source code for the plugin can be viewed at my bitbucket account here. Hope the source helps anyone else looking to build similar things! :)


Harsh J
Blog | Code | Twitter
QwertyManiac, proud to be a member of KDE forums since 2008-Oct.


Bookmarks



Who is online

Registered users: Bing [Bot], daret, Google [Bot], sandyvee, Sogou [Bot]