summaryrefslogtreecommitdiffstats
path: root/languages/sql/sqlactions.cpp
blob: 8cfac095eac263165ed26df656d072eafc17823f (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
/***************************************************************************
 *   Copyright (C) 2003 by Harald Fernengel                                *
 *   [email protected]                                                    *
 *                                                                         *
 *   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 "sqlactions.h"

#include <qpopupmenu.h>
#include <qstringlist.h>
#include <qsqldatabase.h>

#include <kdebug.h>
#include <klocale.h>
#include <ktoolbar.h>
#include <kiconloader.h>
#include <kcombobox.h>

#include "kdevplugin.h"
#include "kdevlanguagesupport.h"
#include "sqlsupport_part.h"

SqlListAction::SqlListAction(SQLSupportPart *part, const QString &text, 
				 const KShortcut& cut,
                                 const QObject *receiver, const char *slot,
                                 KActionCollection *parent, const char *name)
    : KWidgetAction( m_combo = new KComboBox(), text, cut, 0, 0, parent, name), m_part(part)
{
#if (QT_VERSION >= 0x030100)
    m_combo->setEditable( false );
    m_combo->setAutoCompletion( true );
#endif

    m_combo->setMinimumWidth( 200 );
    m_combo->setMaximumWidth( 400 );

    connect( m_combo, SIGNAL(activated(const QString&)), receiver, slot );
    connect( m_combo, SIGNAL(activated(int)), this, SLOT(activated(int)) );

    setShortcutConfigurable( false );
    setAutoSized( true );

    refresh();
}


void SqlListAction::setCurrentConnectionName(const QString &name)
{
    int idx = m_part->connections().findIndex( name );
    if ( idx < 0 )
        m_combo->setCurrentItem( 0 );
    else
        m_combo->setCurrentItem( idx + 1 );
}


QString SqlListAction::currentConnectionName() const
{
    if ( m_combo->currentItem() <= 0 )
        return QString::null;
    return m_part->connections()[ m_combo->currentItem() - 1 ];
}

void SqlListAction::activated(int idx)
{
    if (idx < 1 || (int)m_part->connections().count() <= idx)
        return;
    const QSqlDatabase *db = QSqlDatabase::database(m_part->connections()[idx], true);
    m_combo->changeItem( db->isOpen() ? SmallIcon( "ok" ) : SmallIcon( "no" ), 
                         m_combo->text(idx), idx );
}

void SqlListAction::refresh()
{
    const QStringList& dbc = m_part->connections();

    m_combo->clear();
    m_combo->insertItem( i18n("<no database server>") );

    QString cName;
    for ( QStringList::ConstIterator it = dbc.begin(); it != dbc.end(); ++it ) {

        QSqlDatabase* db = QSqlDatabase::database( (*it), false );
        if ( !db ) {
            kdDebug( 9000 ) << "Could not find database connection " << (*it) << endl;
            m_combo->insertItem( SmallIcon( "no" ), i18n("<error - no connection %1>").arg( *it ) );
            continue;
        }
        cName = db->driverName();
        cName.append( "://" ).append( db->userName() ).append( "@" ).append( db->hostName() );
        cName.append( "/" ).append( db->databaseName() );

        m_combo->insertItem( db->open() ? SmallIcon( "ok" ) : SmallIcon( "no" ), cName );
    }
}


#include "sqlactions.moc"