summaryrefslogtreecommitdiff
path: root/noncore/applets/pcmcia/pcmcia.cpp
blob: c6386cb8ef4265fac5dc43e24c09f216987356fb (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/*
                             This file is part of the Opie Project
             =.             (C) 2005 Michael 'Mickey' Lauer <mickey@Vanille.de>
            .=l.
           .>+-=
 _;:,     .>    :=|.         This program is free software; you can
.> <`_,   >  .   <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--   :           the terms of the GNU Library General Public
.="- .-=="i,     .._         License as published by the Free Software
 - .   .-<_>     .<>         Foundation; either version 2 of the License,
     ._= =}       :          or (at your option) any later version.
    .%`+i>       _;_.
    .i_,=:_.      -<s.       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
..}^=.=       =       ;      Library General Public License for more
++=   -.     .`     .:       details.
 :     =  ...= . :.=-
 -.   .:....=;==+<;          You should have received a copy of the GNU
  -_. . .   )=.  =           Library General Public License along with
    --        :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "pcmcia.h"
#include "configdialog.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/odevice.h>
#include <opie2/oconfig.h>
#include <opie2/oprocess.h>
#include <opie2/opcmciasystem.h>
#include <opie2/oresource.h>
#include <opie2/otaskbarapplet.h>
#include <qpe/applnk.h>
#include <qpe/global.h>
#include <qpe/resource.h>
using namespace Opie::Core;
using namespace Opie::Ui;

/* QT */
#include <qcombobox.h>
#include <qcopchannel_qws.h>
#include <qpainter.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qmessagebox.h>
#include <qsound.h>
#include <qtimer.h>

/* STD */
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#if defined(_OS_LINUX_) || defined(Q_OS_LINUX)
#include <sys/vfs.h>
#include <mntent.h>
#endif

PcmciaManager::PcmciaManager( QWidget * parent ) : QWidget( parent ), popupMenu( 0 )
{
    QCopChannel * pcmciaChannel = new QCopChannel( "QPE/Card", this );
    connect( pcmciaChannel,
             SIGNAL( received(const QCString&,const QByteArray&) ), this,
             SLOT( cardMessage(const QCString&,const QByteArray&) ) );

    setFocusPolicy( NoFocus );
    setFixedWidth ( AppLnk::smallIconSize() );
    setFixedHeight ( AppLnk::smallIconSize() );
    pm = Opie::Core::OResource::loadPixmap( "pcmcia", Opie::Core::OResource::SmallIcon );
    configuring = false;

    QCopChannel *channel = new QCopChannel( "QPE/System", this );
    connect( channel, SIGNAL(received(const QCString&,const QByteArray&)),
                this, SLOT(handleSystemChannel(const QCString&,const QByteArray&)) );
}


PcmciaManager::~PcmciaManager()
{
}

void PcmciaManager::handleSystemChannel( const QCString&msg, const QByteArray& )
{
    if ( msg == "returnFromSuspend()" )
    {
        if ( !OPcmciaSystem::instance()->cardCount() ) return;
        OPcmciaSystem* sys = OPcmciaSystem::instance();
        OPcmciaSystem::CardIterator it = sys->iterator();

        while ( it.current() )
        {
            if ( !it.current()->isEmpty() )
            {
                executeAction( it.current(), "resume" );
            }
        ++it;
        }
    }
}

void PcmciaManager::popUp( QString message, QString icon )
{
    if ( !popupMenu)
    {
        popupMenu = new QPopupMenu( this );
        popupMenu->setFocusPolicy( QWidget::NoFocus );
    }
    popupMenu->clear();

    if ( icon.isEmpty() ) {
        popupMenu->insertItem( message, 0 );
    } else {
        popupMenu->insertItem( QIconSet( Opie::Core::OResource::loadPixmap( icon, Opie::Core::OResource::SmallIcon ) ),
                               message, 0 );
    }

    QPoint p = mapToGlobal( QPoint( 0, 0 ) );
    QSize s = popupMenu->sizeHint();
    popupMenu->popup( QPoint( p.x() + ( width() / 2 ) - ( s.width() / 2 ),
                              p.y() - s.height() ), 0 );

    QTimer::singleShot( 2000, this, SLOT( popupTimeout() ) );
}


void PcmciaManager::popupTimeout()
{
    popupMenu->hide();
}

enum { EJECT, INSERT, SUSPEND, RESUME, RESET, CONFIGURE };
static const char* actionText[] = { "eject", "insert", "suspend", "resum", "resett", "configur" };

void PcmciaManager::mousePressEvent( QMouseEvent* )
{
    QPopupMenu* menu = new QPopupMenu( this );
    QStringList cmd;
    bool execute = true;

    OPcmciaSystem* sys = OPcmciaSystem::instance();
    sys->synchronize();
    OPcmciaSystem::CardIterator it = sys->iterator();
    if ( !sys->count() ) return;

    int i = 0;
    while ( it.current() )
    {
        QPopupMenu* submenu = new QPopupMenu( menu );
        submenu->insertItem( "&Eject",     EJECT+i*100 );
        submenu->insertItem( "&Insert",    INSERT+i*100 );
        submenu->insertItem( "&Suspend",   SUSPEND+i*100 );
        submenu->insertItem( "&Resume",    RESUME+i*100 );
        submenu->insertItem( "Rese&t",     RESET+i*100 );
        submenu->insertItem( "&Configure", CONFIGURE+i*100 );

        bool isSuspended = it.current()->isSuspended();
        bool isEmpty = it.current()->isEmpty();

        submenu->setItemEnabled( EJECT+i*100, !isEmpty );
        submenu->setItemEnabled( INSERT+i*100, isEmpty );
        submenu->setItemEnabled( SUSPEND+i*100, !isEmpty && !isSuspended );
        submenu->setItemEnabled( RESUME+i*100, !isEmpty && isSuspended );
        submenu->setItemEnabled( RESET+i*100, !isEmpty && !isSuspended );
        submenu->setItemEnabled( CONFIGURE+i*100, !isEmpty && !configuring );

        connect( submenu, SIGNAL(activated(int)), this, SLOT(userCardAction(int)) );
        menu->insertItem( tr( "%1: %2" ).arg( i++ ).arg( it.current()->identity() ), submenu, 1 );
        ++it;
    }

    QPoint p = mapToGlobal( QPoint( 0, 0 ) );
    QSize s = menu->sizeHint();
    int opt = menu->exec( QPoint( p.x() + ( width() / 2 ) - ( s.width() / 2 ), p.y() - s.height() ), 0 );
    qDebug( "pcmcia: menu result = %d", opt );
    delete menu;
}


void PcmciaManager::cardMessage( const QCString & msg, const QByteArray & )
{
    odebug << "PcmciaManager::cardMessage( '" << msg << "' )" << oendl;
    if ( msg != "stabChanged()" ) return;

    /* check if a previously unknown card has been inserted */
    OPcmciaSystem::instance()->synchronize();

    if ( !OPcmciaSystem::instance()->cardCount() ) return;

    OConfig cfg( "PCMCIA" );
    cfg.setGroup( "Global" );
    int nCards = cfg.readNumEntry( "nCards", 0 );

    OPcmciaSystem* sys = OPcmciaSystem::instance();
    OPcmciaSystem::CardIterator it = sys->iterator();

    bool newCard = true;
    OPcmciaSocket* theCard = 0;

    while ( it.current() && newCard )
    {
        if ( it.current()->isEmpty() )
        {
            odebug << "pcmcia: skipping empty card in socket " << it.current()->number() << oendl;
            ++it;
            continue;
        }
        else
        {
            theCard = it.current();
            QString cardName = theCard->productIdentity();
            for ( int i = 0; i < nCards; ++i )
            {
                QString cardSection = QString( "Card_%1" ).arg( i );
                cfg.setGroup( cardSection );
                QString name = cfg.readEntry( "name" );
                odebug << "pcmcia: comparing card '" << cardName << "' with known card '" << name << "'" << oendl;
                if ( cardName == name )
                {
                    newCard = false;
                    odebug << "pcmcia: we have seen this card before" << oendl;
                    executeAction( theCard, "insert" );
                    break;
                }
            }
            if ( !newCard ) ++it; else break;
        }
    }
    if ( newCard )
    {
        odebug << "pcmcia: unconfigured card detected" << oendl;
        QString newCardName = theCard->productIdentity();
        int result = QMessageBox::information( qApp->desktop(),
                                           tr( "PCMCIA/CF Subsystem" ),
                                           tr( "<qt>You have inserted the card<br/><b>%1</b><br/>This card is not yet configured. Do you want to configure it now?</qt>" ).arg( newCardName ),
                                           tr( "Yes" ), tr( "No" ), 0, 0, 1 );
        odebug << "pcmcia: result = " << result << oendl;
        if ( result == 0 )
        {
            QString insertAction; QString resumeAction; QString driver; QString conf;
            bool configured = configure( theCard, insertAction, resumeAction, driver, conf );

            if ( configured )
            {
                odebug << "pcmcia: card has been configured. writing out to database" << oendl;
                cfg.setGroup( QString( "Card_%1" ).arg( nCards ) );
                cfg.writeEntry( "name", newCardName );
                cfg.writeEntry( "insertAction", insertAction );
                cfg.writeEntry( "resumeAction", resumeAction );
                cfg.setGroup( "Global" );
                cfg.writeEntry( "nCards", nCards+1 );
                cfg.write();

                QFile confFile( conf );
                if ( confFile.open( IO_ReadWrite | IO_Append ) )
                {
                    QString entryCard = QString( "card \"%1\"" ).arg( newCardName );
                    QString entryVersion( "    version " );
                    for ( QStringList::Iterator it = theCard->productIdentityVector().begin(); it != theCard->productIdentityVector().end(); ++it )
                    {
                        entryVersion += QString( "\"%1\", " ).arg( *it );
                    }
                    QString entryBind = QString( "    bind %1" ).arg( driver );
                    QString entry = QString( "\n%1\n%2\n%3\n" ).arg( entryCard ).arg( entryVersion ).arg( entryBind );
                    odebug << "pcmcia: writing entry...:" << entry << oendl;

                    confFile.writeBlock( (const char*) entry, entry.length() );
                    Global::statusMessage( "restarting pcmcia services..." );
                    ::system( "/etc/init.d/pcmcia restart" );
                }
                else
                {
                    owarn << "pcmcia: couldn't write binding to '" << conf << "' ( " << strerror( errno ) << " )." << oendl;
                }
            }
            else
            {
                odebug << "pcmcia: card has not been configured this time. leaving as unknown card" << oendl;
            }
        }
        else
        {
            odebug << "pcmcia: user doesn't want to configure " << newCardName << " now." << oendl;
        }
    }
    else // it's an already configured card
    {
        odebug << "pcmcia: doing nothing... why do we come here?" << oendl;
    }
}

void PcmciaManager::paintEvent( QPaintEvent * )
{
    QPainter p( this );
    p.drawPixmap( 0, 0, pm );
}

int PcmciaManager::position()
{
    return 7;
}

void PcmciaManager::execCommand( const QStringList &strList )
{
}

void PcmciaManager::userCardAction( int action )
{
    odebug << "pcmcia: user action on socket " << action / 100 << " requested. action = " << action << oendl;

    int socket = action / 100;
    int what = action % 100;
    bool success = false;

    switch ( what )
    {
        case CONFIGURE:
        {
            QString insertAction; QString resumeAction; QString driver; QString conf;
            configure( OPcmciaSystem::instance()->socket( socket ), insertAction, resumeAction, driver, conf );
            return;
        }
        case EJECT:   success = OPcmciaSystem::instance()->socket( socket )->eject();
                      break;
        case INSERT:  success = OPcmciaSystem::instance()->socket( socket )->insert();
                      break;
        case SUSPEND: success = OPcmciaSystem::instance()->socket( socket )->suspend();
                      break;
        case RESUME:  success = OPcmciaSystem::instance()->socket( socket )->resume();
                      break;
        case RESET:   success = OPcmciaSystem::instance()->socket( socket )->reset();
                      break;
        default:      odebug << "pcmcia: not yet implemented" << oendl;
    }

    if ( success )
    {
        popUp( tr( "Successfully %1ed card in socket #%2" ).arg( actionText[action] ).arg( socket ) );
    }
    else
    {
        popUp( tr( "Error while %1ing card in socket #%2" ).arg( actionText[action] ).arg( socket ) );
    }
}

bool PcmciaManager::configure( OPcmciaSocket* card, QString& insertAction, QString& resumeAction, QString& driver, QString& conf )
{
    configuring = true;
    ConfigDialog dialog( card, qApp->desktop() );
    int configresult = QPEApplication::execDialog( &dialog, false );
    configuring = false;
    odebug << "pcmcia: configresult = " << configresult << oendl;
    if ( configresult )
    {
        insertAction = dialog.cbInsertAction->currentText();
        resumeAction = dialog.cbResumeAction->currentText();
        driver = dialog.cbBindTo->currentText();
        conf = dialog.bindEntries[driver];
    }
    return configresult;
}

void PcmciaManager::executeAction( Opie::Core::OPcmciaSocket* card, const QString& type )
{
    odebug << "pcmcia: performing " << type << " action ..." << oendl;
    QString theAction = ConfigDialog::preferredAction( card, type );
    int intAction = card->number() * 100;
    if ( theAction == "activate" ) ;
    else if ( theAction == "eject" ) intAction += EJECT;
    else if ( theAction == "suspend" ) intAction += SUSPEND;
    else if ( theAction == "prompt for" )
    {
        odebug << "pcmcia: sorry, not 'prompt for' is not yet implemented!" << oendl;
        return;
    }
    else
    {
        owarn << "pcmcia: action '" << theAction << "' not known. Huh?" << oendl;
        return;
    }
    userCardAction( intAction );
}

EXPORT_OPIE_APPLET_v1( PcmciaManager )