Commit 2db268b2 authored by Matt Edman's avatar Matt Edman
Browse files

Refactor the saving-to-file code a bit;

Make the default log filename not have any spaces in it and give it a .txt
extension.


svn:r176
parent 4c8d514d
Loading
Loading
Loading
Loading
+19 −46
Changes for src/gui/messagelog/messagelog.cpp: 19 added lines, 46 removed lines.
Original line number Diff line number Diff line
@@ -275,24 +275,19 @@ MessageLog::_filterLog()
  }
}

/**
 Saves currently selected messages to a file
**/
/** Saves the given list of items to a file */
void
MessageLog::saveSelected()
MessageLog::_save(QList<QTreeWidgetItem *> items)
{
  /* Do nothing if there are no selected messages */
  QList<QTreeWidgetItem *> selected = ui.lstMessages->selectedItems();
  int count = selected.size();
  
  if (!count) {
  if (!items.size()) {
    return;
  }

  QString fileName = QFileDialog::getSaveFileName(this,
                          tr("Save Selected Messages"),
                          "Vidalia Log " + 
                          _clock->currentDateTime().toString("MM.dd.yyyy"));
                          tr("Save Log Messages"),
                          "VidaliaLog-" + 
                          _clock->currentDateTime().toString("MM.dd.yyyy") +
                          ".txt");
  
  /* If the choose to save */
  if (!fileName.isEmpty()) {
@@ -311,9 +306,9 @@ MessageLog::saveSelected()
    QTextStream out(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    
    for (int i=0; i < count; i++) {
    foreach (QTreeWidgetItem *item, items) {
      for (int j=0; j < ui.lstMessages->columnCount(); j++) {
        out << selected[i]->text(j) << "    ";
        out << item->text(j) << "    ";
      }
      out << "\n";
    }
@@ -322,43 +317,21 @@ MessageLog::saveSelected()
}

/**
 Saves all (shown) messages to a file
 Saves currently selected messages to a file
**/
void
MessageLog::saveAll()
MessageLog::saveSelected()
{
  QString fileName = QFileDialog::getSaveFileName(this,
                          tr("Save All Messages"),
                          "Vidalia Log " + 
                          _clock->currentDateTime().toString("MM.dd.yyyy"));
  
  /* If the choose to save */
  if (!fileName.isEmpty()) {
    QFile file(fileName);
    
    /* If can't write to file, show error message */
    if (!file.open(QFile::WriteOnly | QFile::Text)) {
      QMessageBox::warning(this, tr("Vidalia"),
                          tr("Cannot write file %1:\n%2.")
                          .arg(fileName)
                          .arg(file.errorString()));
      return;
    }
    
    /* Write out the message log to the file */
    QTextStream out(&file);
    QApplication::setOverrideCursor(Qt::WaitCursor);
    int count = ui.lstMessages->topLevelItemCount();
    
    for (int i=0; i < count; i++) {
      for (int j=0; j < ui.lstMessages->columnCount(); j++) {
        out << ui.lstMessages->topLevelItem(i)->text(j) << "    ";
      }
      out << "\n";
  _save(ui.lstMessages->selectedItems());
}

    QApplication::restoreOverrideCursor();
  }
/**
 Saves all (shown) messages to a file
**/
void
MessageLog::saveAll()
{
  _save(ui.lstMessages->findItems("*", Qt::MatchWildcard));
}

/** 
+3 −0
Changes for src/gui/messagelog/messagelog.h: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -109,6 +109,9 @@ private:
  /** Registers the current message filter with Tor */
  void _registerLogEvents();
  
  /** Saves the given list of items to a file */
  void _save(QList<QTreeWidgetItem *> items);
  
  /** Holds the maximum number of messages to log **/
  int _maxCount;