diff --git a/history.cc b/history.cc index 999c6b94..b90d5959 100644 --- a/history.cc +++ b/history.cc @@ -53,7 +53,7 @@ void History::addItem( Item const & item ) if( !enabled() ) return; - if ( item.word.size() > 60 || item.word.isEmpty() ) + if ( item.word.size() > MAX_HISTORY_ITEM_LENGTH || item.word.isEmpty() ) { // The search looks bogus. Don't save it. return; diff --git a/history.hh b/history.hh index a46a4a5f..76e0cd51 100644 --- a/history.hh +++ b/history.hh @@ -8,6 +8,8 @@ #include #include +#define MAX_HISTORY_ITEM_LENGTH 60 + /// Search history class History: public QObject { @@ -74,6 +76,9 @@ public: bool enabled() { return addingEnabled; } + unsigned getMaxSize() + { return maxSize; } + signals: /// Signals the changes in items in response to addItem() or clear(). diff --git a/mainwindow.cc b/mainwindow.cc index e2ccce0f..a26eb24b 100644 --- a/mainwindow.cc +++ b/mainwindow.cc @@ -19,6 +19,7 @@ #include #include "dprintf.hh" #include +#include #ifdef Q_OS_MAC #include "lionsupport.h" @@ -2652,9 +2653,11 @@ static bool needHideSearchPane; ui.wordList->clear(); history.enableAdd( true ); + ui.importHistory->setDisabled( false ); } else { + ui.importHistory->setDisabled( true ); history.enableAdd( false ); disconnect( ui.translateLine, SIGNAL( textChanged( QString const & ) ), @@ -2740,7 +2743,65 @@ void MainWindow::on_exportHistory_activated() mainStatusBar->showMessage( tr( "History export complete" ), 5000 ); return; } - QString errStr = QString( tr( "Export error: ") ) + file.errorString(); + QString errStr = QString( tr( "Export error: " ) ) + file.errorString(); file.close(); mainStatusBar->showMessage( errStr, 10000, QPixmap( ":/icons/error.png" ) ); } + +void MainWindow::on_importHistory_activated() +{ + QString fileName = QFileDialog::getOpenFileName( this, tr( "Import history from file" ), + QDir::homePath(), + tr( "Text files (*.txt);;All files (*.*)" ) ); + if( fileName.size() == 0) + return; + + QString errStr; + QFile file( fileName ); + + for(;;) + { + if ( !file.open( QFile::ReadOnly | QIODevice::Text ) ) + break; + + QTextStream fileStream( & file ); + QString itemStr, trimmedStr; + QList< QString > itemList; + + history.clear(); + + do + { + itemStr = fileStream.readLine(); + if( fileStream.status() >= QTextStream::ReadCorruptData ) + break; + + trimmedStr = itemStr.trimmed(); + if( trimmedStr.isEmpty() ) + continue; + + if( trimmedStr.size() <= MAX_HISTORY_ITEM_LENGTH ) + itemList.prepend( trimmedStr ); + + } while( !fileStream.atEnd() && itemList.size() < (int)history.getMaxSize() ); + + for( QList< QString >::const_iterator i = itemList.constBegin(); i != itemList.constEnd(); ++i ) + history.addItem( History::Item( 1, *i ) ); + + if( file.error() != QFile::NoError ) + break; + + if( fileStream.status() >= QTextStream::ReadCorruptData ) + { + errStr = QString ( tr( "Import error: invalid data in file" ) ); + mainStatusBar->showMessage( errStr, 10000, QPixmap( ":/icons/error.png" ) ); + } + else + mainStatusBar->showMessage( tr( "History import complete" ), 5000 ); + return; + } + errStr = QString( tr( "Import error: " ) ) + file.errorString(); + file.close(); + mainStatusBar->showMessage( errStr, 10000, QPixmap( ":/icons/error.png" ) ); +} + diff --git a/mainwindow.hh b/mainwindow.hh index 7ac16064..0f64c279 100644 --- a/mainwindow.hh +++ b/mainwindow.hh @@ -326,6 +326,7 @@ private slots: void on_showHideHistory_activated(); void on_exportHistory_activated(); + void on_importHistory_activated(); }; #endif diff --git a/mainwindow.ui b/mainwindow.ui index caaf4630..a594a269 100644 --- a/mainwindow.ui +++ b/mainwindow.ui @@ -116,6 +116,7 @@ + @@ -426,6 +427,11 @@ &Export + + + &Import + +