summaryrefslogtreecommitdiff
path: root/noncore/apps/checkbook/qcheckmainmenu.cpp
blob: 1aead88dce158994fc4b6e0765ae56681a74143d (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
#include "qcheckmainmenu.h"
#include "qcheckname.h"

QCheckMainMenu::QCheckMainMenu(QWidget *parent)
  : QCheckMMBase(parent)
{
  init();
}

void QCheckMainMenu::init()
{
  lstCheckBooks->clear();
  QString checkdirname = QDir::homeDirPath();
  checkdirname.append("/.checkbooks");
  QDir checkdir(checkdirname);
  if (checkdir.exists() == true)
  {
    QStringList checkbooks = checkdir.entryList("*.qcb", QDir::Files|QDir::Readable|QDir::Writable, QDir::Time);
    for (QStringList::Iterator it = checkbooks.begin(); it != checkbooks.end(); it++)
    {
      (*it) = (*it).remove((*it).find('.'), (*it).length());
    }
    lstCheckBooks->insertStringList(checkbooks);
  }
  lstCheckBooks->clearSelection();
  connect(lstCheckBooks, SIGNAL(clicked(QListBoxItem *)), this, SLOT(slotSelected(QListBoxItem *)));
  lstCheckBooks->clearSelection();
}

void QCheckMainMenu::slotSelected(QListBoxItem *item)
{
  if (item != 0)
  {
    QString text = item->text();
    if (text.isEmpty() == false)
    {
      text.append(".qcb");
      QString checkdirname = QDir::homeDirPath();
      checkdirname.append("/.checkbooks/");
      text.prepend(checkdirname);
      emit itemSelected(text);
    }
  }
}

void QCheckMainMenu::newClicked()
{
  QString checkname = QCheckName::getName();
  if (checkname.isEmpty() == false)
  {
    QString checkdirname = QDir::homeDirPath();
    checkdirname.append("/.checkbooks");
    QDir checkdir(checkdirname);
    if (checkdir.exists() == false)
    {
      checkdir.mkdir(checkdirname);
    }
    checkdirname.append("/");
    checkdirname.append(checkname);
    checkdirname.append(".qcb");
    QFile file(checkdirname);
    if (file.exists() == false)
    {
      file.open(IO_WriteOnly);
      QTextStream os(&file);
      os << "";
      file.close();
    }
    QFileInfo fi(file);
    QString noextension = fi.fileName();
    noextension = noextension.remove(noextension.find('.'), noextension.length());
    lstCheckBooks->insertItem(noextension);
  }
}

void QCheckMainMenu::deleteClicked()
{
    QString checkname = lstCheckBooks->currentText();
    if (checkname.isEmpty() == false)   {
        switch ( QMessageBox::warning(this,tr("Delete Account"),tr("Really delete the\n")
                                      +checkname+tr(" account?")
                                      ,tr("Yes"),tr("No"),0,1,1) ) {
          case 0: {

              QString checkdirname = QDir::homeDirPath();
              checkdirname.append("/.checkbooks");
              QDir checkdir(checkdirname);
              QString checkDir=checkdirname;
              
              checkdirname.append("/");
              checkdirname.append(checkname);
              checkdirname.append(".qcb");
              QFile file(checkdirname);
              if (file.exists() == true) {
                  if(!file.remove()) {
                      QMessageBox::message(tr("Checkbook"),tr("Could not remove account"));
                      return;
                  } else
                      lstCheckBooks->removeItem(lstCheckBooks->currentItem());
              }
          }
              break;
        }
    }
}