]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/qt-console/restore/restore.cpp
Addressed issue of long compiles. Simplify with Pages::setCurrent
[bacula/bacula] / bacula / src / qt-console / restore / restore.cpp
index 144a17667a019a18605fcf20a2ccefda97590d0a..4cdb7470fe4cb589338e69de53833263ab53ab0e 100644 (file)
 #include "bat.h"
 #include "restore.h"
 
-restoreDialog::restoreDialog(Console *console)
+restorePage::restorePage(Console *console)
 {
    QStringList titles;
-   m_console = console;
-  
-   m_console->setEnabled(false);
+
    setupUi(this);
+   QTreeWidgetItem *parent = mainWin->getFromHash(console);
+   if (!parent) {
+      /* Make this a user configurable stdout msg *** FIXME ****/
+      printf("Error retrieving tree widget.");
+      return;
+   }
+   m_name = "Restore";
+   pgInitialize(parent);
+   m_console->notify(false);          /* this should already be off */
+   m_closeable = true;
+
    connect(fileWidget, SIGNAL(itemDoubleClicked(QTreeWidgetItem*, int)), 
            this, SLOT(fileDoubleClicked(QTreeWidgetItem *, int)));
+   connect(directoryWidget, SIGNAL(
+           currentItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)),
+           this, SLOT(directoryItemChanged(QTreeWidgetItem *, QTreeWidgetItem *)));
    connect(upButton, SIGNAL(pressed()), this, SLOT(upButtonPushed()));
    connect(markButton, SIGNAL(pressed()), this, SLOT(markButtonPushed()));
    connect(unmarkButton, SIGNAL(pressed()), this, SLOT(unmarkButtonPushed()));
+   connect(okButton, SIGNAL(pressed()), this, SLOT(okButtonPushed()));
+   connect(cancelButton, SIGNAL(pressed()), this, SLOT(cancelButtonPushed()));
    setFont(m_console->get_font());
    m_console->displayToPrompt();
 
-
    titles << "Mark" << "File" << "Mode" << "User" << "Group" << "Size" << "Date";
    fileWidget->setHeaderLabels(titles);
 
    get_cwd();
+
+   //QString root("");
+   //addDirectory(root);
    fillDirectory();
+   dockPage();
+   setCurrent();
    this->show();
 }
 
 /*
  * Fill the fileWidget box with the contents of the current directory
  */
-void restoreDialog::fillDirectory()
+void restorePage::fillDirectory()
 {
    char modes[20], user[20], group[20], size[20], date[30];
    char marked[10];
@@ -75,7 +93,7 @@ void restoreDialog::fillDirectory()
 
    fileWidget->clear();
    m_console->write_dir("dir");
-   QList<QTreeWidgetItem *> items;
+   QList<QTreeWidgetItem *> treeItemList;
    QStringList item;
    while (m_console->read() > 0) {
       char *p = m_console->msg();
@@ -123,38 +141,117 @@ void restoreDialog::fillDirectory()
       split_path_and_filename(p, &path, &pnl, &file, &fnl);
       item.clear();
       item << marked << file << modes << user << group << size << date;
+      if (item[1].endsWith("/")) {
+         addDirectory(item[1]);
+       }
       QTreeWidgetItem *ti = new QTreeWidgetItem((QTreeWidget *)0, item);
       ti->setTextAlignment(5, Qt::AlignRight); /* right align size */
-      items.append(ti);
+      treeItemList.append(ti);
    }
    fileWidget->clear();
-   fileWidget->insertTopLevelItems(0, items);
+   fileWidget->insertTopLevelItems(0, treeItemList);
+   for (int i=0; i<7; i++) {
+      fileWidget->resizeColumnToContents(i);
+   }
 
    free_pool_memory(file);
    free_pool_memory(path);
 }
 
-void restoreDialog::accept()
+/*
+ * Function called from fill directory when a directory is found to see if this
+ * directory exists in the directory pane and then add it to the directory pane
+ */
+void restorePage::addDirectory(QString &newdirr)
+{
+   QString newdir = newdirr;
+   QString fullpath = m_cwd + newdirr;
+   QRegExp regex("^/[a-z]:/$");
+   bool ok=true;
+
+   //printf("In addDirectory cwd \"%s\" newdir \"%s\"\n", m_cwd.toUtf8().data(),
+        //newdir.toUtf8().data());
+
+   /* add unix '/' directory first */
+   if (m_dirPaths.empty() && (regex.indexIn(fullpath,0) == -1)) {
+      QTreeWidgetItem *item = new QTreeWidgetItem(directoryWidget);
+      QString text("/");
+      item->setText(0, text.toUtf8().data());
+      //printf("Pre Inserting %s\n",text.toUtf8().data());
+      m_dirPaths.insert(text, item);
+      m_dirTreeItems.insert(item, text);
+   }
+
+   if (regex.indexIn(fullpath,0) == 0) {
+      /* this is a windows drive */
+      //printf("Need to do windows c:/\n");
+      fullpath.replace(0,1,"");
+   }
+   /* is it already existent ?? */
+   if (!m_dirPaths.contains(fullpath)) {
+      QTreeWidgetItem *item = NULL;
+      if (m_dirPaths.empty()) {
+         /* this is the base widget */
+         item = new QTreeWidgetItem(directoryWidget);
+         item->setText(0, fullpath.toUtf8().data());
+      } else {
+         QTreeWidgetItem *parent = m_dirPaths.value(m_cwd);
+         if (parent) {
+            /* new directories to add */
+            item = new QTreeWidgetItem(parent);
+            item->setText(0, newdir.toUtf8().data());
+            directoryWidget->expandItem(parent);
+         } else {
+            ok=false;
+            //printf("In else of if parent cwd \"%s\" newdir \"%s\"\n", 
+               //m_cwd.toUtf8().data() ,newdir.toUtf8().data());
+         }
+      }
+      /* insert into both forward and reverse hash */
+      if (ok) {
+         //printf("Inserting %s\n",fullpath.toUtf8().data());
+         m_dirPaths.insert(fullpath, item);
+         m_dirTreeItems.insert(item, fullpath);
+      }
+   }
+}
+
+/*
+ * Executed when the tree item in the directory pane is changed.  This will
+ * allow us to populate the file pane and make this the cwd.
+ */
+void restorePage::directoryItemChanged(QTreeWidgetItem *currentitem,
+                                         QTreeWidgetItem * /*previousitem*/)
+{
+   QString fullpath = m_dirTreeItems.value(currentitem);
+   if (fullpath != ""){
+      cwd(fullpath.toUtf8().data());
+      fillDirectory();
+   }
+}
+
+void restorePage::okButtonPushed()
 {
    this->hide();
    m_console->write("done");
-   delete this;
-   m_console->setEnabled(true);
+   m_console->notify(true);
+   closeStackPage();
    mainWin->resetFocus();
 }
 
 
-void restoreDialog::reject()
+void restorePage::cancelButtonPushed()
 {
    this->hide();
    m_console->write("quit");
    mainWin->set_status("Canceled");
-   delete this;
-   m_console->setEnabled(true);
+   closeStackPage();
+   m_console->notify(true);
    mainWin->resetFocus();
 }
 
-void restoreDialog::fileDoubleClicked(QTreeWidgetItem *item, int column)
+void restorePage::fileDoubleClicked(QTreeWidgetItem *item, int column)
 {
    char cmd[1000];
    if (column == 0) {                 /* mark/unmark */
@@ -178,23 +275,43 @@ void restoreDialog::fileDoubleClicked(QTreeWidgetItem *item, int column)
     *  the directory -- or nothing if it is not a directory.
     */
    if (item->text(1).endsWith("/")) {
-      cwd(item->text(1).toUtf8().data());
-      fillDirectory();
+      QString fullpath = m_cwd + item->text(1);
+      /* check for fullpath = "/c:/" */
+      QRegExp regex("^/[a-z]:/");
+      if (regex.indexIn(fullpath,0) == 0)  /* remove leading '/' */
+         fullpath.replace(0,1,"");
+      QTreeWidgetItem *item = m_dirPaths.value(fullpath);
+      if (item) {
+         directoryWidget->setCurrentItem(item);
+      } else {
+         /* FIXME ***** Create an error log */
+         //printf("DoubleClick else of item column %i fullpath %s\n", column, fullpath.toUtf8().data());
+      }
    }
 }
 
-void restoreDialog::upButtonPushed()
+/*
+ * If up button pushed, making the parent tree widget current will call fill
+ * directory.
+ */
+void restorePage::upButtonPushed()
 {
    cwd("..");
-   fillDirectory();
+   QTreeWidgetItem *item = m_dirPaths.value(m_cwd);
+   if (item) {
+      directoryWidget->setCurrentItem(item);
+   }
 }
 
-void restoreDialog::markButtonPushed()
+/*
+ * Mark selected items
+ */
+void restorePage::markButtonPushed()
 {
-   QList<QTreeWidgetItem *> items = fileWidget->selectedItems();
+   QList<QTreeWidgetItem *> treeItemList = fileWidget->selectedItems();
    QTreeWidgetItem *item;
    char cmd[1000];
-   foreach (item, items) {
+   foreach (item, treeItemList) {
       bsnprintf(cmd, sizeof(cmd), "mark \"%s\"", item->text(1).toUtf8().data());
       item->setText(0, "*");
       m_console->write_dir(cmd);
@@ -207,12 +324,15 @@ void restoreDialog::markButtonPushed()
    }
 }
 
-void restoreDialog::unmarkButtonPushed()
+/*
+ * Unmark selected items
+ */
+void restorePage::unmarkButtonPushed()
 {
-   QList<QTreeWidgetItem *> items = fileWidget->selectedItems();
+   QList<QTreeWidgetItem *> treeItemList = fileWidget->selectedItems();
    QTreeWidgetItem *item;
    char cmd[1000];
-   foreach (item, items) {
+   foreach (item, treeItemList) {
       bsnprintf(cmd, sizeof(cmd), "unmark \"%s\"", item->text(1).toUtf8().data());
       item->setText(0, " ");
       m_console->write_dir(cmd);
@@ -228,7 +348,7 @@ void restoreDialog::unmarkButtonPushed()
 /*
  * Change current working directory 
  */
-bool restoreDialog::cwd(const char *dir)
+bool restorePage::cwd(const char *dir)
 {
    int stat;
    char cd_cmd[MAXSTRING];
@@ -236,22 +356,23 @@ bool restoreDialog::cwd(const char *dir)
    bsnprintf(cd_cmd, sizeof(cd_cmd), "cd \"%s\"", dir);
    Dmsg2(100, "dir=%s cmd=%s\n", dir, cd_cmd);
    m_console->write_dir(cd_cmd);
+   lineEdit->clear();
    if ((stat = m_console->read()) > 0) {
       m_cwd = m_console->msg();
+      lineEdit->insert(m_cwd);
       Dmsg2(100, "cwd=%s msg=%s\n", m_cwd.toUtf8().data(), m_console->msg());
    } else {
       Dmsg1(000, "stat=%d\n", stat);
+      QMessageBox::critical(this, "Error", "cd command failed", QMessageBox::Ok);
    }
    m_console->discardToPrompt();
-   lineEdit->clear();
-   lineEdit->insert(m_cwd);
    return true;  /* ***FIXME*** return real status */
 }
 
 /*
  * Return cwd when in tree restore mode 
  */
-char *restoreDialog::get_cwd()
+char *restorePage::get_cwd()
 {
    int stat;
    m_console->write_dir(".pwd");
@@ -261,7 +382,9 @@ char *restoreDialog::get_cwd()
       Dmsg2(100, "cwd=%s msg=%s\n", m_cwd.toUtf8().data(), m_console->msg());
    } else {
       Dmsg1(000, "stat=%d\n", stat);
+      QMessageBox::critical(this, "Error", ".pwd command failed", QMessageBox::Ok);
+      Dmsg1(000, "stat=%d\n", stat);
    }
-   m_console->displayToPrompt(); 
+   m_console->discardToPrompt(); 
    return m_cwd.toUtf8().data();
 }