summaryrefslogtreecommitdiff
path: root/noncore/apps/opie-bartender/bartender.cpp
blob: 57b28d4a53e0affa09358e1575b15146bce18247 (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
395
396
397
398
399
400
401
402
/****************************************************************************
**
** Created: Sat Jul 20 08:10:53 2002
**      by:  L.J. Potter <ljp@llornkcor.com>
**     copyright            : (C) 2002 by ljp
    email                : ljp@llornkcor.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 "bartender.h"
#include "showdrinks.h"
#include "inputDialog.h"
#include "searchresults.h"
#include "bac.h"
#include "drinkdata.h"

/* OPIE */
#include <opie2/odebug.h>
#include <opie2/oresource.h>
#include <qpe/qpeapplication.h>
using namespace Opie::Core;

/* QT */
#include <qlineedit.h>
#include <qdir.h>
#include <qpushbutton.h>
#include <qlistbox.h>
#include <qmultilineedit.h>
#include <qmessagebox.h>
#include <qtextstream.h>
#include <qaction.h>
#include <qheader.h>
#include <qlistview.h>
#include <qlayout.h>
#include <qtoolbar.h>
#include <qmenubar.h>

/* STD */
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>

Bartender::Bartender( QWidget* parent,  const char* name, WFlags fl )
    : QMainWindow( parent, name, fl ) {
    if ( !name )
        setName( "Bartender" );

    connect( qApp,SIGNAL( aboutToQuit()),SLOT( cleanUp()) );

    setCaption( tr( "Bartender" ) );

    setToolBarsMovable( false );
    QToolBar *dock = new QToolBar( this );
    dock->setHorizontalStretchable( true );

    QMenuBar *menuBar = new QMenuBar( dock );
    menuBar->setMargin( 0 );
    QPopupMenu *fileMenu;
    fileMenu = new QPopupMenu( this);
    menuBar->insertItem( tr("File"), fileMenu );

    fileMenu->insertItem(tr("New Drink"));
    fileMenu->insertItem(tr("Open Drink"));
    fileMenu->insertItem(tr("Find by Drink Name"));
    fileMenu->insertItem(tr("Find by Ingredient"));

    QPopupMenu *editMenu;
    editMenu = new QPopupMenu( this);
    menuBar->insertItem( tr("Edit"), editMenu );
    editMenu->insertItem(tr("Edit Drink"));

    connect( fileMenu, SIGNAL( activated(int) ), this, SLOT( fileMenuActivated(int) ));
    connect( editMenu, SIGNAL( activated(int) ), this, SLOT( editMenuActivated(int) ));

    ToolBar1 = new QToolBar( this );

    QAction *a = new QAction( tr( "New" ), Opie::Core::OResource::loadPixmap( "new", Opie::Core::OResource::SmallIcon ),
                              "New", 0, this, 0 );
    connect( a, SIGNAL( activated() ), this, SLOT( fileNew() ) );
    a->addTo( ToolBar1 );

    a = new QAction( tr( "Open" ), Opie::Core::OResource::loadPixmap( "bartender/bartender_sm", Opie::Core::OResource::SmallIcon ),
                     "open", 0, this, 0 );
    connect( a, SIGNAL( activated() ), this, SLOT( openCurrentDrink() ) );
    a->addTo( ToolBar1 );

    a = new QAction( tr( "Find" ), Opie::Core::OResource::loadPixmap( "find", Opie::Core::OResource::SmallIcon ),
                     "Find", 0, this, 0 );
    connect( a, SIGNAL( activated() ), this, SLOT( askSearch() ) );
    a->addTo( ToolBar1 );

    a = new QAction( tr( "Edit" ), Opie::Core::OResource::loadPixmap( "edit", Opie::Core::OResource::SmallIcon ),
                     "Edit", 0, this, 0 );
    connect( a, SIGNAL( activated() ), this, SLOT( doEdit() ) );
    a->addTo( ToolBar1 );

    QPushButton *t;
    t= new QPushButton( "BAC", ToolBar1, "bacButtin");
    connect( t, SIGNAL( clicked() ), this, SLOT( doBac() ) );

    DrinkView = new QListView( this, "DrinkView" );
    DrinkView->addColumn( tr( "Name of Drink" ) );
    DrinkView->header()->hide();

    QPEApplication::setStylusOperation( DrinkView->viewport(),QPEApplication::RightOnHold);

    connect(DrinkView, SIGNAL( doubleClicked(QListViewItem*)),this,SLOT(showDrink(QListViewItem*)));
    connect(DrinkView, SIGNAL( mouseButtonPressed(int,QListViewItem*,const QPoint&,int)),
            this,SLOT( showDrink(int,QListViewItem*,const QPoint&,int)));

    if(QDir("db").exists())
        drinkDB.setFile("db/drinkdb.txt");
    else
        drinkDB.setFile(QPEApplication::qpeDir()+"etc/bartender/drinkdb.txt");
    
    initDrinkDb();

    DrinkView->setFocus();

    setCentralWidget( DrinkView );
}

Bartender::~Bartender() {
}

/*
this happens right before exit  */
void Bartender::cleanUp() {

}

void Bartender::initDrinkDb() {
    if(drinkDB.read())
        fillList();
    else
        QMessageBox::message( (tr("Note")), (tr("Drink database not opened sucessfully.\n")) );
}

void Bartender::fillList() {
    QString lastName;
    if( DrinkView->currentItem() != NULL )
        lastName = DrinkView->currentItem()->text(0);
    else
        lastName = "";

    DrinkView->clear();
    int i=0;
    QListViewItem * item ;
    QString s;
    
    DrinkList::Iterator it = drinkDB.getBegin();
    while ( it != drinkDB.getEnd() ) {
        item= new QListViewItem( DrinkView, 0 );
        item->setText( 0, (*it).getName());
        i++;
        ++it;
    }
    
    if( lastName != "" ) {
        QListViewItemIterator it( DrinkView );
        for ( ; it.current(); ++it ) {
            if ( it.current()->text(0) == lastName ) {
                DrinkView->setCurrentItem(it.current());
                break;
            }
        }
    }
    else if( DrinkView->childCount() > 0)
        DrinkView->setCurrentItem(DrinkView->firstChild());
    
    odebug << "there are currently " << i << " of drinks" << oendl; 
}

void Bartender::fileNew() {

    New_Drink *newDrinks;
    newDrinks = new New_Drink(this,"New Drink", TRUE);
    QString newName, newIng;
    QPEApplication::execDialog( newDrinks );
    newName = newDrinks->LineEdit1->text();
    newIng= newDrinks->MultiLineEdit1->text();

    if(newDrinks ->result() == 1 ) {
        drinkDB.addDrink(newName, newIng);
        if(!drinkDB.writeChanges()) {
            QMessageBox::message( (tr("Note")), (tr("Failed to write to drink database!\n")) );
        }
        fillList();
    }
    delete newDrinks;
}

void Bartender::showDrink(int mouse, QListViewItem * item, const QPoint&, int) {
    switch (mouse) {
      case 1:
//          showDrink(item);
          break;
      case 2:
          showDrink(item);
          break;
    }
}

void Bartender::showDrink( QListViewItem *item) {
    if(item==NULL) return;
    Show_Drink *showDrinks;
    QString myDrink=item->text(0);
    showDrinks = new Show_Drink(this, myDrink, TRUE);

    DrinkList::Iterator it = drinkDB.findDrink(myDrink);
    if(it != drinkDB.getEnd()) {
        showDrinks->MultiLineEdit1->setText((*it).getIngredients());
    
        connect(showDrinks->editDrinkButton, SIGNAL(clicked()), this, SLOT(doEdit()));
        QPEApplication::execDialog( showDrinks );
    }

    delete showDrinks;
}

void Bartender::askSearch() {
    switch ( QMessageBox::warning(this,tr("Find"),tr("Search by drink name\n")+
                                  "\nor ingredient ?"
                                  ,tr("Drink Name"),tr("Ingredient"),0,0,1) ) {
      case 0:
          doSearchByName();
          break;
      case 1:
          doSearchByIngredient();
          break;
    };
}

/* search by name */
void Bartender::doSearchByName() {
//    if( DrinkView->currentItem() == NULL) return;
    QStringList searchList;
    QString searchForDrinkName;
    InputDialog *fileDlg;
    fileDlg = new InputDialog(this,tr("Find by Drink Name"),TRUE, 0);
    fileDlg->exec();
    if( fileDlg->result() == 1 ) {
        searchForDrinkName = fileDlg->LineEdit1->text();
        QListViewItemIterator it( DrinkView );
        for ( ; it.current(); ++it ) {
            if ( it.current()->text(0).find( searchForDrinkName, 0, FALSE) != -1 ) {
//                 odebug << it.current()->text(0) << oendl; 
                searchList.append(it.current()->text(0));
            }
        }
        if(searchList.count() >0)
            showSearchResult(searchList);
        else
            QMessageBox::message(tr("Search"),tr("Sorry no results for\n")+searchForDrinkName);
    }//end Inputdialog
    delete fileDlg;
}

void Bartender::doSearchByIngredient() {
//    if( DrinkView->currentItem() == NULL) return;
    QStringList searchList;
    QString searchForIngredient, lastDrinkName, lastDrinkIngredients;

    InputDialog *fileDlg;
    fileDlg = new InputDialog(this,tr("Find by Ingredient"),TRUE, 0);
    fileDlg->exec();
    if( fileDlg->result() == 1 ) {
        searchForIngredient = fileDlg->LineEdit1->text();

        DrinkList::Iterator it = drinkDB.getBegin();
        while ( it != drinkDB.getEnd() ) {
            lastDrinkName = (*it).getName();
            lastDrinkIngredients = (*it).getIngredients();
            
            if( lastDrinkIngredients.find( searchForIngredient ,0, FALSE) != -1 )
                searchList.append( lastDrinkName );
            
            ++it;
        }
        
        if(searchList.count() >0)
            showSearchResult(searchList);
        else
            QMessageBox::message(tr("Search"),tr("Sorry no results for\n")+ searchForIngredient);
    }
    delete fileDlg;
}

void Bartender::showSearchResult(QStringList &searchList) {
    QString result;
    Search_Results *searchDlg;

    searchList.sort();

    searchDlg = new Search_Results(this, "Search Results", TRUE);
    searchDlg->ListBox1->insertStringList( searchList,-1);
    QPEApplication::execDialog( searchDlg );

    if( searchDlg->result() == 1 ) {
        result= searchDlg->ListBox1->currentText();
    }
    QListViewItemIterator it2( DrinkView );
    for ( ; it2.current(); ++it2 ) {
        if ( it2.current()->text(0)== result ) {
//                odebug << it2.current()->text(0) << oendl; 
            showDrink(it2.current());
        }
    }
delete  searchDlg;
}

void Bartender::doEdit() {
    if(DrinkView->currentItem() == NULL) {
      fileNew();
      return;
    }

    QString myDrink;
    myDrink= DrinkView->currentItem()->text(0);
    
    New_Drink *newDrinks;
    newDrinks = new New_Drink(this,"Edit Drink", TRUE);
    QString newName, newIng;
    QPEApplication::showDialog( newDrinks );

    DrinkList::Iterator drinkItem = drinkDB.findDrink(myDrink);
    if(drinkItem != drinkDB.getEnd()) {
        newDrinks->LineEdit1->setText((*drinkItem).getName());
        newDrinks->MultiLineEdit1->setText((*drinkItem).getIngredients());
        
        newDrinks->exec();
        newName = newDrinks->LineEdit1->text();
        newIng= newDrinks->MultiLineEdit1->text();
    
        if( newDrinks ->result() == 1 ) {
            (*drinkItem).setName(newName);
            (*drinkItem).setIngredients(newIng);
            if(!drinkDB.writeChanges()) {
                QMessageBox::message( (tr("Note")), (tr("Failed to write to drink database!\n")) );
            }
            fillList();
        }
    }
    
    delete newDrinks;
}

void Bartender::clearList() {
    DrinkView->clear();
}

void Bartender::doBac() {
    BacDialog *bacDlg;
    bacDlg = new BacDialog(this,"BAC",TRUE);
    QPEApplication::execDialog( bacDlg );
    delete bacDlg;
}

void Bartender::openCurrentDrink() {
    if(DrinkView->currentItem() == NULL) return;
    showDrink(DrinkView->currentItem());
}

void Bartender::fileMenuActivated( int item) {
    odebug << "Item " << item << "" << oendl; 
    switch(item) {
      case -3: //        new -3
          fileNew();
          break;
      case -4://        open -4
          openCurrentDrink();
          break;
      case -5://        drink -5
          doSearchByName();

          break;
      case -6://        alcohol -6
          doSearchByIngredient();

          break;

    }
}

void Bartender::editMenuActivated(int item) {
    odebug << "Item " << item << "" << oendl; 
      /*
        edit -8
      */
    switch(item) {
      case -8:
          doEdit() ;
          break;

    }
}