summaryrefslogtreecommitdiffstats
path: root/languages/cpp/app_templates/kpartplugin/plugin_app.cpp
blob: c5bdb434a7fd51caed163bd74cf27ac1b5d2ca09 (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

#include "plugin_%{APPNAMELC}.h"

#include <khtml_part.h>
#include <kaction.h>
#include <kinstance.h>
#include <kmessagebox.h>
#include <klocale.h>
#include <kglobal.h>

Plugin%{APPNAME}::Plugin%{APPNAME}( TQObject* parent, const char* name )
    : Plugin( parent, name )
{
    // Instantiate all of your actions here.  These will appear in
    // Konqueror's menu and toolbars.
    (void) new KAction( i18n("&Plugin Action"), "%{APPNAMELC}", 0,
                        this, TQT_SLOT(slotAction()),
                        actionCollection(), "plugin_action" );
}

Plugin%{APPNAME}::~Plugin%{APPNAME}()
{
}

void Plugin%{APPNAME}::slotAction()
{
    // This plugin assumes KHTMLPart.  If your plugin can handle more
    // than this or a different Part than this, simply delete or
    // change the following block.
    if ( !parent()->inherits("KHTMLPart") )
    {
        TQString title( i18n( "Cannot Translate Source" ) );
        TQString text( i18n( "You cannot translate anything except web pages "
                            "with this plugin." ) );

        KMessageBox::sorry( 0, text, title );
        return;
    }

    // Get a handle on our parent so we may get the necessary data for
    // processing
    KHTMLPart *part = dynamic_cast<KHTMLPart *>(parent());

    // This plugin only uses the URL.  You may use whatever data you
    // need.
    KURL url( part->url() );

    // This is a standard check to make sure we are dealing with a
    // valid URL
    if ( !url.isValid() )
    {
        TQString title( i18n( "Malformed URL" ) );
        TQString text( i18n( "The URL you entered is not valid, please "
                            "correct it and try again" ) );

        KMessageBox::sorry( 0, text, title );
        return;
    }

// The following block is very plugin specific.  In this example, we
// translate the current page with AltaVista's BabelFish.  You will
// definitely want to change this.
// BEGIN
    KURL work( "http://babel.altavista.com/translate.dyn" );

    TQString query( "urltext=" );
    query += KURL::encode_string( url.url() );
    work.setQuery( query );
// END

    // Finally, execute the request
    part->openURL( work );
}

%{APPNAME}Factory::%{APPNAME}Factory()
  : KLibFactory()
{
}

%{APPNAME}Factory::~%{APPNAME}Factory()
{
  delete s_instance;
  s_instance = 0;
}

TQObject* %{APPNAME}Factory::createObject( TQObject* parent, const char* name, const char*, const TQStringList & )
{
  return new Plugin%{APPNAME}( parent, name );
}

TDEInstance *%{APPNAME}Factory::instance()
{
  if ( !s_instance )
    s_instance = new TDEInstance( "%{APPNAMELC}" );
  return s_instance;
}

extern "C"
{
  void* init_lib%{APPNAMELC}plugin()
  {
    TDEGlobal::locale()->insertCatalogue("%{APPNAMELC}");
    return new %{APPNAME}Factory;
  }
}

TDEInstance* %{APPNAME}Factory::s_instance = 0L;

#include "plugin_%{APPNAMELC}.moc"