summaryrefslogtreecommitdiffstats
path: root/parts/documentation/find_documentation.cpp
blob: ee6c639aeb9fd9515fc264c9a4f8cedceca719db (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***************************************************************************
 *   Copyright (C) 2004 by Tobias Glaesser                                 *
 *   tobi.web@gmx.de                                                       *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   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; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.             *
 ***************************************************************************/
#include "find_documentation.h"

#include <stdlib.h>
#include <tqcheckbox.h>
#include <tqheader.h>
#include <tqapplication.h>
#include <tqstringlist.h>

#include <klineedit.h>
#include <kprocess.h>
#include <tdelistbox.h>

#include <kdevpartcontroller.h>
#include <kdevdocumentationplugin.h>

#include "documentation_widget.h"
#include "documentation_part.h"
#include "indexview.h"
#include "find_documentation_options.h"
#include "docutils.h"
#include "contentsview.h"

#include <tdelocale.h>

FindDocumentation::FindDocumentation(DocumentationWidget* parent, const char* name)
    :FindDocumentationBase(parent, name),
    man_item(0), info_item(0), index_item(0), google_item(0), contents_item(0),
    last_item(0), m_widget(parent)
{
    TQWidget* tmp = TQApplication::desktop();
    setGeometry(tmp->width()/2 - width()/2, tmp->height()/2 - height()/2, width(), height());
    proc_man = new TDEProcess( this );
    proc_info = new TDEProcess( this );

    connect( proc_man, TQT_SIGNAL(processExited( TDEProcess* )),
                this, TQT_SLOT(procManExited( TDEProcess* )) );
    connect( proc_man, TQT_SIGNAL(receivedStdout( TDEProcess*, char*, int)),
		this, TQT_SLOT(procManReadStdout( TDEProcess*, char*, int)) );

    connect( proc_info, TQT_SIGNAL(processExited( TDEProcess* )),
                this, TQT_SLOT(procInfoExited( TDEProcess* )) );
    connect( proc_info, TQT_SIGNAL(receivedStdout( TDEProcess*, char*, int)),
		this, TQT_SLOT(procInfoReadStdout( TDEProcess*, char*, int)) );

    result_list->header()->hide();
    result_list->setSorting(-1);

    m_options = new FindDocumentationOptions(this);
}

FindDocumentation::~FindDocumentation()
{
}

/*$SPECIALIZATION$*/
void FindDocumentation::buttonPressedOnItem( int button, TQListViewItem * item, const TQPoint & pos, int // c
                                             )
{
    if ((button != TQt::RightButton) || (!item))
        return;
    DocumentationItem *docItem = dynamic_cast<DocumentationItem*>(item);
    if (!docItem)
        return;

    DocUtils::docItemPopup(m_widget->part(), docItem->text(0), docItem->url(), pos, true, false);
}

void FindDocumentation::clickOnItem( TQListViewItem * item )
{
    if(!item)
        return;

    if(item == man_item || item == info_item || item == index_item || item == google_item)
        return;

    DocumentationItem* doc_item = dynamic_cast<DocumentationItem*>(item);

    if(item->parent() == man_item ||
        item->parent() == info_item ||
        item->parent() == google_item ||
        item->parent() == index_item ||
        item->parent() == contents_item)
        m_widget->part()->partController()->showDocument(doc_item->url());
}

void FindDocumentation::procInfoExited( TDEProcess* )
{
    if (proc_info->normalExit() && proc_info->exitStatus() == 0)
    {
        TQStringList lines = TQStringList::split("\n", proc_info_out);
        for( TQStringList::const_iterator it = lines.begin(); it != lines.end(); ++it )
        {
            if( (*it) == "*")
                break;

            DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document, info_item, *it);
            newitem->setURL(KURL("info:/" + search_term->text()));
        }
    }
    proc_info_out = "";

    if(info_item->firstChild() && m_options->goto_first_match->isOn())
    {
        m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(info_item->firstChild())->url());
        first_match_found = true;
    }
}

void FindDocumentation::procManExited( TDEProcess* )
{
    if (proc_man->normalExit() && proc_man->exitStatus() == 0)
    {
        TQStringList lines = TQStringList::split("\n", proc_man_out);
        for( TQStringList::const_iterator it = lines.begin(); it != lines.end(); ++it )
        {
            DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document, man_item, search_term->text());
            newitem->setURL(KURL("man://" + *it));
        }
    }
    proc_man_out = "";

    if(man_item->firstChild() && m_options->goto_first_match->isOn())
    {
        m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(man_item->firstChild())->url());
        first_match_found = true;
    }
}

void FindDocumentation::procInfoReadStdout( TDEProcess*, char* buf, int len)
{
    proc_info_out += TQString::fromLatin1( buf, len );
}

void FindDocumentation::procManReadStdout( TDEProcess*, char* buf, int len)
{
    proc_man_out += TQString::fromLatin1( buf, len );
}

void FindDocumentation::searchInInfo()
{
    info_item = new TDEListViewItem(result_list, last_item, "Info");
    info_item->setOpen(true);
    last_item = info_item;

    proc_info->clearArguments();
    //Search Info documents
    *proc_info << "info";
    *proc_info << "-w";
    *proc_info << search_term->text();

    if ( !proc_info->start( TDEProcess::NotifyOnExit, TDEProcess::Stdout ) )
    {
        // error handling
    }
}

void FindDocumentation::searchInMan()
{
    man_item = new TDEListViewItem( result_list, last_item, i18n("Manual") );
    man_item->setOpen(true);
    last_item = man_item;

    proc_man->clearArguments();
    //Search Manpages
    *proc_man << "man";
    *proc_man << "-w";
    *proc_man << search_term->text();

    if ( !proc_man->start( TDEProcess::NotifyOnExit, TDEProcess::Stdout ) )
    {
        // error handling
    }

}

void FindDocumentation::searchInGoogle()
{
    google_item = new TDEListViewItem(result_list, last_item, "Google");
    google_item->setOpen(true);
    last_item = google_item;

    DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document,
        google_item, "First result for: " + search_term->text());
    newitem->setURL(KURL("http://www.google.com/search?q=" + search_term->text() + "&btnI"));
    newitem = new DocumentationItem(DocumentationItem::Document, google_item, "All results for: " + search_term->text());
    newitem->setURL(KURL("http://www.google.com/search?q=" + search_term->text()));

    if (google_item->firstChild() && m_options->goto_first_match->isOn())
    {
        m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(google_item->firstChild())->url());
        first_match_found = true;
    }
}

void FindDocumentation::searchInContents()
{
    contents_item = new TDEListViewItem( result_list, last_item, i18n("Contents") );
    contents_item->setOpen(true);
    last_item = contents_item;

    TQListViewItemIterator it( m_widget->m_contents->m_view );
    while ( it.current() )
    {
        DocumentationItem *docItem = dynamic_cast<DocumentationItem*>(it.current());

        if (docItem->type() == DocumentationItem::Catalog)
        {
            DocumentationCatalogItem *catalogItem = dynamic_cast<DocumentationCatalogItem*>(it.current());
            catalogItem->load();
            catalogItem->plugin()->setCatalogURL(catalogItem);
        }
        if (it.current()->text(0).contains(search_term->text(),false))
        {
            DocumentationItem* newitem = new DocumentationItem(DocumentationItem::Document,
                contents_item, it.current()->text(0) );
            newitem->setURL(docItem->url());
        }
        ++it;
    }

    if (contents_item->firstChild() && m_options->goto_first_match->isOn())
    {
        m_widget->part()->partController()->showDocument(dynamic_cast<DocumentationItem*>(contents_item->firstChild())->url());
        first_match_found = true;
    }
}

void FindDocumentation::searchInIndex()
{
    index_item =new TDEListViewItem(result_list, last_item , "Index");
    index_item->setOpen(true);
    last_item = index_item;

    m_widget->part()->emitIndexSelected(m_widget->m_index->indexBox());
    m_widget->m_index->setSearchTerm(search_term->text());
    m_widget->m_index->showIndex(search_term->text());

    if(m_widget->m_index->m_index->selectedItem())
    {
        IndexItem* item = dynamic_cast<IndexItem*>(m_widget->m_index->m_index->selectedItem());
        DocumentationItem* newitem = 0;
        while(item)
        {
            if(!item->text().contains(search_term->text(),false))
                break;

            IndexItem::List urls = item->urls();
            for (IndexItem::List::const_iterator it = urls.begin(); it != urls.end(); ++it)
            {
                TQString text = item->text();
                if (urls.count() > 1)
                    text = (*it).first;
                if(newitem)
                    newitem = new DocumentationItem(DocumentationItem::Document, index_item,
                        newitem, text);
                else
                    newitem = new DocumentationItem(DocumentationItem::Document,
                        index_item, text);

                newitem->setURL((*it).second);
            }

            item = dynamic_cast<IndexItem*>(item->next());
        }
    }

    if(index_item->firstChild() && m_options->goto_first_match->isOn())
    {
        m_widget->part()->partController()->showDocument(
            dynamic_cast<DocumentationItem*>(index_item->firstChild())->url());
        first_match_found = true;
    }
}

void FindDocumentation::startSearch()
{
    //Clear the contents of the listview
    result_list->clear();

    last_item = NULL;
    first_match_found = false;

    TQListViewItem* item = m_options->source_list->firstChild();

    while ( item && ( !m_options->goto_first_match->isOn() || !first_match_found ))
    {
        if ( m_options->isMan(dynamic_cast<TQCheckListItem*>(item)) )
            searchInMan();
        else if ( m_options->isInfo(dynamic_cast<TQCheckListItem*>(item)) )
            searchInInfo();
        else if ( m_options->isIndex(dynamic_cast<TQCheckListItem*>(item)) )
            searchInIndex();
        else if ( m_options->isGoogle(dynamic_cast<TQCheckListItem*>(item)) )
            searchInGoogle();
        else if ( m_options->isContents(dynamic_cast<TQCheckListItem*>(item)) )
            searchInContents();

        item = item->itemBelow();
    }

    //Set the focus to the listview and the first child to the
    //current item of the list, so that you can brows the items with your keyboard
    result_list->setFocus();
    if(result_list->firstChild())
      result_list->setCurrentItem(result_list->firstChild());

}

void FindDocumentation::setSearchTerm( const TQString & term )
{
    search_term->setText(term);
}

void FindDocumentation::clickOptions()
{
    if( m_options->exec() == TQDialog::Accepted )
        m_options->writeOptions();
    else
        m_options->readOptions();
}

void FindDocumentation::focusInEvent( TQFocusEvent * // e
                                      )
{
    search_term->setFocus();
}

#include "find_documentation.moc"