summaryrefslogtreecommitdiffstats
path: root/languages/perl/perlsupportpart.cpp
blob: 3236e72e07e4ae83d500c948363aae31500f13c0 (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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/***************************************************************************
 *   Copyright (C) 2001-2002 by Bernd Gehrmann                             *
 *   bernd@kdevelop.org                                                    *
 *                                                                         *
 *   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 "perlsupportpart.h"

#include <tqfileinfo.h>
#include <tqpopupmenu.h>
#include <tqstringlist.h>
#include <tqtextstream.h>
#include <tqtimer.h>
#include <tdeaction.h>
#include <tdeapplication.h>
#include <kdebug.h>
#include <kdevgenericfactory.h>
#include <kinputdialog.h>
#include <tdelocale.h>
#include <tqregexp.h>
#include <codemodel.h>
#include <tqprogressbar.h>
#include <kstatusbar.h>
#include "kdevmainwindow.h"

#include <kprocess.h>
#include <stdlib.h>
#include <unistd.h>

#include "kdevcore.h"
#include "kdevproject.h"
#include "kdevpartcontroller.h"
#include "kdevplugininfo.h"
#include "kdevappfrontend.h"
//#include "classstore.h"
//#include "parsedclass.h"
//#include "parsedmethod.h"
//#include "parsedscript.h"
#include "domutil.h"
//#include "programmingbycontract.h"

typedef KDevGenericFactory<PerlSupportPart> PerlSupportFactory;
static const KDevPluginInfo data("kdevperlsupport");
K_EXPORT_COMPONENT_FACTORY( libkdevperlsupport, PerlSupportFactory( data ) )

PerlSupportPart::PerlSupportPart(TQObject *parent, const char *name, const TQStringList &)
    : KDevLanguageSupport(&data, parent, name ? name : "PerlSupportPart")
{
    setInstance(PerlSupportFactory::instance());

    setXMLFile("kdevperlsupport.rc");

    connect( core(), TQT_SIGNAL(projectOpened()), this, TQT_SLOT(projectOpened()) );
    connect( core(), TQT_SIGNAL(projectClosed()), this, TQT_SLOT(projectClosed()) );
    connect( partController(), TQT_SIGNAL(savedFile(const KURL&)),
             this, TQT_SLOT(savedFile(const KURL&)) );

    TDEAction *action;

    action = new TDEAction( i18n("Execute Main Program"), "application-x-executable", 0,
                          this, TQT_SLOT(slotExecute()),
                          actionCollection(), "build_exec" );
    action->setToolTip( i18n("Runs the Perl program") );

    action = new TDEAction( i18n("Execute String..."), "application-x-executable", 0,
                          this, TQT_SLOT(slotExecuteString()),
                          actionCollection(), "build_execstring" );
    action->setToolTip( i18n("Executes a string as Perl code") );

    action = new TDEAction( i18n("Start Perl Interpreter"), "application-x-executable", 0,
                          this, TQT_SLOT(slotStartInterpreter()),
                          actionCollection(), "build_runinterpreter" );
    action->setToolTip( i18n("Starts the Perl interpreter without a program") );

    action = new TDEAction( i18n("Find Perl Function Documentation..."), 0,
                          this, TQT_SLOT(slotPerldocFunction()),
                          actionCollection(), "help_perldocfunction" );
    action->setToolTip( i18n("Show the documentation page of a Perl function") );

    action = new TDEAction( i18n("Find Perl FAQ Entry..."), 0,
                          this, TQT_SLOT(slotPerldocFAQ()),
                          actionCollection(), "help_perldocfaq" );
    action->setToolTip( i18n("Show the FAQ entry for a keyword") );

    //perl parser for codemodel
    m_parser = new  perlparser(core(),codeModel(),interpreter());
}


PerlSupportPart::~PerlSupportPart()
{
  if (project())
      projectClosed();

  delete m_parser;
  m_parser=0;
}


void PerlSupportPart::projectOpened()
{
    kdDebug(9007) << "projectOpened()" << endl;

    connect( project(), TQT_SIGNAL(addedFilesToProject(const TQStringList &)),
             this, TQT_SLOT(addedFilesToProject(const TQStringList &)) );
    connect( project(), TQT_SIGNAL(removedFilesFromProject(const TQStringList &)),
             this, TQT_SLOT(removedFilesFromProject(const TQStringList &)) );

    // We want to parse only after all components have been
    // properly initialized
    TQTimer::singleShot(0, this, TQT_SLOT(initialParse()));
}


void PerlSupportPart::projectClosed()
{
}

void PerlSupportPart::maybeParse(const TQString fileName)
{
    TQFileInfo fi(fileName);
    TQString path = fi.filePath();
    TQString extension = fi.extension();
    if (extension == "pl" || extension == "pm") {
        kdDebug(9016) << "maybe " << fileName << endl;
        removeWithReference(fileName);
        m_parser->parse(fileName);
	emit addedSourceInfo( fileName);
    }
}

void PerlSupportPart::addedFilesToProject(const TQStringList &fileList)
{
	kdDebug(9016) << "addedFilesToProject()" << endl;

	TQStringList::ConstIterator it;

	for ( it = fileList.begin(); it != fileList.end(); ++it )
	{
		maybeParse(project()->projectDirectory() + "/" + ( *it ));
	}
}


void PerlSupportPart::removedFilesFromProject(const TQStringList &fileList)
{
    kdDebug(9016) << "removedFilesFromProject()" << endl;
    TQStringList::ConstIterator it;
    for ( it = fileList.begin(); it != fileList.end(); ++it )
    {
     TQString fileName = project()->projectDirectory() + "/" + ( *it );
     removeWithReference(fileName);
   }
  emit updatedSourceInfo();
}


void PerlSupportPart::savedFile(const KURL &fileName)
{
    Q_UNUSED( fileName.path() );
#if 0  // not needed anymore
    kdDebug(9016) << "savedFile()" << endl;

    if (project()->allFiles().contains(fileName.mid ( project()->projectDirectory().length() + 1 ))) {
        maybeParse(fileName);
        emit updatedSourceInfo();
    }
#endif
}


KDevLanguageSupport::Features PerlSupportPart::features()
{
    return KDevLanguageSupport::Features(Classes | Functions | Variables | Namespaces | /*Scripts | */NewClass | AddMethod | AddAttribute /*| NewScript*/);
//    return Functions;
}

TQString PerlSupportPart::interpreter()
{
    TQString prog = DomUtil::readEntry(*projectDom(), "/kdevperlsupport/run/interpreter");
    if (prog.isEmpty())
        prog = "perl";

    return prog;
}


void PerlSupportPart::startApplication(const TQString &program)
{
    bool inTerminal = DomUtil::readBoolEntry(*projectDom(), "/kdevperlsupport/run/terminal");
    if (KDevAppFrontend *appFrontend = extension<KDevAppFrontend>("TDevelop/AppFrontend"))
        appFrontend->startAppCommand(TQString(), program, inTerminal);
}


void PerlSupportPart::slotExecute()
{
    TQString program =  project()->mainProgram();
    TQString cmd = interpreter() + " " + program;
    startApplication(cmd);
}


void PerlSupportPart::slotStartInterpreter()
{
    startApplication(interpreter());
}


void PerlSupportPart::slotExecuteString()
{
    bool ok;
    TQString cmd = KInputDialog::getText(i18n("String to Execute"), i18n("String to execute:"), TQString(), &ok, 0);
    if (ok) {
        cmd.prepend("'");
        cmd.append("'");
        startApplication(cmd);
    }
}


void PerlSupportPart::slotPerldocFunction()
{
    bool ok;
    TQString key = KInputDialog::getText(i18n("Show Perl Documentation"), i18n("Show Perl documentation for function:"), "", &ok, 0);
    if (ok && !key.isEmpty()) {
        TQString url = "perldoc:functions/";
        url += key;
        partController()->showDocument(KURL(url));
    }
}


void PerlSupportPart::slotPerldocFAQ()
{
    bool ok;
    TQString key = KInputDialog::getText(i18n("Show FAQ Entry"), i18n("Show FAQ entry for keyword:"), "", &ok, 0);
    if (ok && !key.isEmpty()) {
        TQString url = "perldoc:faq/";
        url += key;
        partController()->showDocument(KURL(url));
    }
}
KMimeType::List PerlSupportPart::mimeTypes( )
{
    KMimeType::List list;
    KMimeType::Ptr mime = KMimeType::mimeType( "application/x-perl" );
    if( mime )
	list << mime;
    return list;
}

void PerlSupportPart::initialParse()
{
    kdDebug(9016) << "initialParse()" << endl;

    if (project()) {
        //copy from cpp support : give user some feedback
        mainWindow()->statusBar()->message( i18n("Updating...") );
        kapp->processEvents( );

        kapp->setOverrideCursor(waitCursor);
        TQStringList files = project()->allFiles();
        m_parser->initialParse();

        //progress bar
        TQProgressBar* bar = new TQProgressBar( files.count( ), mainWindow( )->statusBar( ) );
        bar->setMinimumWidth( 120 );
        bar->setCenterIndicator( true );
        mainWindow( )->statusBar( )->addWidget( bar );
        bar->show( );
        int n = 0;
        for (TQStringList::Iterator it = files.begin(); it != files.end() ;++it) {
//            kdDebug(9016) << "maybe parse " << project()->projectDirectory() + "/" + (*it) << endl;
            maybeParse(project()->projectDirectory() + "/" + *it);
            //update progress bar
            bar->setProgress( n++ );
            if( (n%5) == 0 )
              kapp->processEvents();
        }
        parseUseFiles();
        emit updatedSourceInfo();

        //remove progressbar
        mainWindow( )->statusBar( )->removeWidget( bar );
        delete bar;
        kapp->restoreOverrideCursor();
        mainWindow()->statusBar()->message( i18n("Done") );

    } else {
        kdDebug(9016) << "No project" << endl;
    }
}

void PerlSupportPart::removeWithReference( const TQString & fileName )
{
    kdDebug(9016) << "remove with references: " << fileName << endl;
    //m_timestamp.remove( fileName );
    if( !codeModel()->hasFile(fileName) )
        return;

    emit aboutToRemoveSourceInfo( fileName );
    codeModel()->removeFile( codeModel()->fileByName(fileName) );
}

void PerlSupportPart::parseUseFiles()
{
 kdDebug(9016) << "parse addional libs" << endl;
 return;
 TQString filename;
 TQStringList m_usefiles = m_parser->UseFiles();

 //parse addional use files
 for (TQStringList::Iterator it = m_usefiles.begin(); it != m_usefiles.end() ;++it)
 {
        filename = m_parser->findLib(*it);
       //if something found , parse it
       if (!filename.isEmpty()) {
            //kdDebug(9016) << "found " << filename << endl;
            maybeParse(filename);
       }
 }
}

#include "perlsupportpart.moc"