summaryrefslogtreecommitdiffstats
path: root/kate/openheader/plugin_kateopenheader.cpp
blob: 15dbb77acc507a334b6b83588b0e7c6b0cdc77b0 (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
/***************************************************************************
                          plugin_katetextfilter.cpp  -  description
                             -------------------
    begin                : FRE Feb 23 2001
    copyright            : (C) 2001 by Joseph Wenninger
    email                : jowenn@bigfoot.com
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "plugin_kateopenheader.h"
#include "plugin_kateopenheader.moc"

#include <tqfileinfo.h>
#include <kgenericfactory.h>
#include <kaction.h>
#include <klocale.h>
#include <kdebug.h>
#include <kurl.h>
#include <kio/netaccess.h>

class PluginView : public KXMLGUIClient
{
  friend class PluginKateOpenHeader;

  public:
    Kate::MainWindow *win;
};

K_EXPORT_COMPONENT_FACTORY( kateopenheaderplugin, KGenericFactory<PluginKateOpenHeader>( "kateopenheader" ) )

PluginKateOpenHeader::PluginKateOpenHeader( TQObject* tqparent, const char* name, const TQStringList& )
    : Kate::Plugin ( (Kate::Application *)tqparent, name )
{
}

PluginKateOpenHeader::~PluginKateOpenHeader()
{
}

void PluginKateOpenHeader::addView(Kate::MainWindow *win)
{
    // TODO: doesn't this have to be deleted?
    PluginView *view = new PluginView ();

    (void) new KAction( i18n("Open .h/.cpp/.c"), Key_F12,
      this, TQT_SLOT( slotOpenHeader() ),
      view->actionCollection(), "file_openheader" );

    view->setInstance (new KInstance("kate"));
    view->setXMLFile( "plugins/kateopenheader/ui.rc" );
    win->guiFactory()->addClient (view);
    view->win = win;

   m_views.append (view);
}

void PluginKateOpenHeader::removeView(Kate::MainWindow *win)
{
  for (uint z=0; z < m_views.count(); z++)
    if (m_views.at(z)->win == win)
    {
      PluginView *view = m_views.at(z);
      m_views.remove (view);
      win->guiFactory()->removeClient (view);
      delete view;
    }
}

void PluginKateOpenHeader::slotOpenHeader ()
{
  if (!application()->activeMainWindow())
    return;

  Kate::View * kv (application()->activeMainWindow()->viewManager()->activeView());
  if (!kv) return;

  KURL url=kv->document()->url();
  if ((!url.isValid()) || (url.isEmpty())) return;

  TQFileInfo info( url.path() );
  TQString extension = info.extension().lower();

  TQStringList headers( TQStringList() << "h" << "H" << "hh" << "hpp" );
  TQStringList sources( TQStringList() << "c" << "cpp" << "cc" << "cp" << "cxx" );

  if( sources.tqfind( extension ) != sources.end() ) {
    tryOpen( url, headers );
  } else if ( headers.tqfind( extension ) != headers.end() ) {
    tryOpen( url, sources );
  }
}

void PluginKateOpenHeader::tryOpen( const KURL& url, const TQStringList& extensions )
{
  if (!application()->activeMainWindow())
    return;

  kdDebug() << "Trying to open " << url.prettyURL() << " with extensions " << extensions.join(" ") << endl;
  TQString basename = TQFileInfo( url.path() ).baseName();
  KURL newURL( url );
  for( TQStringList::ConstIterator it = extensions.begin(); it != extensions.end(); ++it ) {
    newURL.setFileName( basename + "." + *it );
    if( KIO::NetAccess::exists( newURL ) )
      application()->activeMainWindow()->viewManager()->openURL( newURL );
    newURL.setFileName( basename + "." + (*it).upper() );
    if( KIO::NetAccess::exists( newURL ) )
      application()->activeMainWindow()->viewManager()->openURL( newURL );
  }
}