summaryrefslogtreecommitdiffstats
path: root/kdesktop/kwebdesktop/kwebdesktop.cpp
blob: 885f1ce5bd175c5b0d965f38d30d7ace27c0c673 (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
/* This file is part of the KDE project
   Copyright (C) 2000 David Faure <faure@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License
   version 2 as published by the Free Software Foundation.

   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.
*/

// Idea by Gael Duval
// Implementation by David Faure

#include <config.h>

#include <tdeapplication.h>
#include <tdeconfig.h>
#include <kdebug.h>
#include <klocale.h>
#include <tdecmdlineargs.h>
#include <tdeaboutdata.h>
#include <kurifilter.h>
#include <tdeio/job.h>

#include <tqscrollview.h>
#include "kwebdesktop.h"
#include <kmimetype.h>
#include <tdeparts/componentfactory.h>
#include "kwebdesktopsettings.h"

#include "kwebdesktop.moc"

static TDECmdLineOptions options[] =
{
  { "+width", I18N_NOOP("Width of the image to create"), 0 },
  { "+height", I18N_NOOP("Height of the image to create"), 0 },
  { "+file", I18N_NOOP("File sname where to dump the output in png format"), 0 },
  { "+[URL]", I18N_NOOP("URL to open (if not specified, it is read from kwebdesktoprc)"), 0 },
  TDECmdLineLastOption
};

KWebDesktopRun::KWebDesktopRun( KWebDesktop* webDesktop, const KURL & url )
    : m_webDesktop(webDesktop), m_url(url)
{
    kdDebug() << "KWebDesktopRun::KWebDesktopRun starting get" << endl;
    TDEIO::Job * job = TDEIO::get(m_url, false, false);
    connect( job, TQT_SIGNAL( result( TDEIO::Job *)),
             this, TQT_SLOT( slotFinished(TDEIO::Job *)));
    connect( job, TQT_SIGNAL( mimetype( TDEIO::Job *, const TQString &)),
             this, TQT_SLOT( slotMimetype(TDEIO::Job *, const TQString &)));
}

void KWebDesktopRun::slotMimetype( TDEIO::Job *job, const TQString &_type )
{
    TDEIO::SimpleJob *sjob = static_cast<TDEIO::SimpleJob *>(job);
    // Update our URL in case of a redirection
    m_url = sjob->url();
    TQString type = _type; // necessary copy if we plan to use it
    sjob->putOnHold();
    kdDebug() << "slotMimetype : " << type << endl;

    KParts::ReadOnlyPart* part = m_webDesktop->createPart( type );
    // Now open the URL in the part
    if ( part )
        part->openURL( m_url );
}

void KWebDesktopRun::slotFinished( TDEIO::Job * job )
{
    // The whole point of all this is to abort silently on error
    if (job->error())
    {
        kdDebug() << job->errorString() << endl;
        kapp->exit(1);
    }
}


int main( int argc, char **argv )
{
    TDEAboutData data( "kwebdesktop", I18N_NOOP("TDE Web Desktop"),
                     VERSION,
                     I18N_NOOP("Displays an HTML page as the background of the desktop"),
                     TDEAboutData::License_GPL,
                     "(c) 2000, David Faure <faure@kde.org>" );
    data.addAuthor( "David Faure", I18N_NOOP("developer and maintainer"), "faure@kde.org" );

    TDECmdLineArgs::init( argc, argv, &data );

    TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.

    TDEApplication app;

    TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();
    if ( args->count() != 3 && args->count() != 4 )
    {
       args->usage();
       return 1;
    }
    const int width = TQCString(args->arg(0)).toInt();
    const int height = TQCString(args->arg(1)).toInt();
    TQCString imageFile = args->arg(2);
    TQString url;
    if (args->count() == 4)
        url = TQString::fromLocal8Bit(args->arg(3));

    KWebDesktop *webDesktop = new KWebDesktop( 0, imageFile, width, height );

    if (url.isEmpty())
      url = KWebDesktopSettings::uRL();
    // Apply uri filter
    KURIFilterData uridata = url;
    KURIFilter::self()->filterURI( uridata );
    KURL u = uridata.uri();

    // Now start getting, to ensure mimetype and possible connection
    KWebDesktopRun * run = new KWebDesktopRun( webDesktop, u );

    int ret = app.exec();

    TDEIO::SimpleJob::removeOnHold(); // Kill any slave that was put on hold.
    delete webDesktop;
    delete run;
    //tdehtml::Cache::clear();

    return ret;
}

void KWebDesktop::slotCompleted()
{
    kdDebug() << "KWebDesktop::slotCompleted" << endl;
    // Dump image to m_imageFile
    TQPixmap snapshot = TQPixmap::grabWidget( m_part->widget() );
    snapshot.save( m_imageFile, "PNG" );
    // And terminate the app.
    kapp->quit();
}

KParts::ReadOnlyPart* KWebDesktop::createPart( const TQString& mimeType )
{
    delete m_part;
    m_part = 0;

    KMimeType::Ptr mime = KMimeType::mimeType( mimeType );
    if ( !mime || mime == KMimeType::defaultMimeTypePtr() )
        return 0;
    if ( mime->is( "text/html" ) || mime->is( "text/xml" ) || mime->is( "application/xhtml+xml" ) )
    {
        TDEHTMLPart* htmlPart = new TDEHTMLPart;
        htmlPart->widget()->resize(m_width,m_height);

        htmlPart->setMetaRefreshEnabled(false);
        htmlPart->setJScriptEnabled(false);
        htmlPart->setJavaEnabled(false);
        htmlPart->setPluginsEnabled(false);

        ((TQScrollView *)htmlPart->widget())->setFrameStyle( TQFrame::NoFrame );
        ((TQScrollView *)htmlPart->widget())->setHScrollBarMode( TQScrollView::AlwaysOff );
        ((TQScrollView *)htmlPart->widget())->setVScrollBarMode( TQScrollView::AlwaysOff );

        connect( htmlPart, TQT_SIGNAL( completed() ), this, TQT_SLOT( slotCompleted() ) );
        m_part = htmlPart;
    } else {
        // Try to find an appropriate viewer component
        m_part = KParts::ComponentFactory::createPartInstanceFromQuery<KParts::ReadOnlyPart>
                 ( mimeType, TQString(), 0, 0, this, 0 );
        if ( !m_part )
            kdWarning() << "No handler found for " << mimeType << endl;
        else {
            kdDebug() << "Loaded " << m_part->className() << endl;
            connect( m_part, TQT_SIGNAL( completed() ),
                     this, TQT_SLOT( slotCompleted() ) );
        }
    }
    if ( m_part ) {
        connect( m_part, TQT_SIGNAL( canceled(const TQString &) ),
                 this, TQT_SLOT( slotCompleted() ) );
    }
    return m_part;
}

KWebDesktop::~KWebDesktop()
{
    delete m_part;
}