mirror of
https://github.com/xiaoyifang/goldendict-ng.git
synced 2024-11-27 19:24:08 +00:00
Import history from file
This commit is contained in:
parent
22a540ae02
commit
b3439fc4a0
|
@ -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;
|
||||
|
|
|
@ -8,6 +8,8 @@
|
|||
#include <QList>
|
||||
#include <QString>
|
||||
|
||||
#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().
|
||||
|
|
|
@ -19,6 +19,7 @@
|
|||
#include <map>
|
||||
#include "dprintf.hh"
|
||||
#include <QDebug>
|
||||
#include <QTextStream>
|
||||
|
||||
#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 & ) ),
|
||||
|
@ -2744,3 +2747,61 @@ void MainWindow::on_exportHistory_activated()
|
|||
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" ) );
|
||||
}
|
||||
|
||||
|
|
|
@ -326,6 +326,7 @@ private slots:
|
|||
|
||||
void on_showHideHistory_activated();
|
||||
void on_exportHistory_activated();
|
||||
void on_importHistory_activated();
|
||||
};
|
||||
|
||||
#endif
|
||||
|
|
|
@ -116,6 +116,7 @@
|
|||
</property>
|
||||
<addaction name="showHideHistory"/>
|
||||
<addaction name="exportHistory"/>
|
||||
<addaction name="importHistory"/>
|
||||
<addaction name="separator"/>
|
||||
<addaction name="clearHistory"/>
|
||||
</widget>
|
||||
|
@ -426,6 +427,11 @@
|
|||
<string>&Export</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="importHistory">
|
||||
<property name="text">
|
||||
<string>&Import</string>
|
||||
</property>
|
||||
</action>
|
||||
</widget>
|
||||
<customwidgets>
|
||||
<customwidget>
|
||||
|
|
Loading…
Reference in a new issue