00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 extern magic_t magic;
00025
00026 void ImportMealMasterDialog::init()
00027 {
00028 databaseWizard = false;
00029 }
00030
00031
00032 void ImportMealMasterDialog::openErrorFile()
00033 {
00034
00035 QString fileName =
00036 checkOverWrite
00037 ( KFileDialog::getSaveFileName
00038 ( ":export",
00039 i18n( "*.mm *.mmf|UTF-8 Mealmaster (*.mm *.mmf)\n"
00040 "*|All Files (*)" ),
00041 this,
00042 i18n( "Save Erroneous Mealmaster Recipes" ) ), this );
00043
00044 if ( fileName != QString::null ) {
00045 errorFileNameEdit->setText( fileName );
00046 updateEnableState();
00047 };
00048 }
00049
00050 void ImportMealMasterDialog::openMealMasterFile()
00051 {
00052
00053 std::string startAt;
00054 if ( databaseWizard ) {
00055 std::string fileName( (const char *)i18n( "scripts/english.utf8.mmf" ) );
00056 startAt = findAnyMealFile( "appdata", fileName.c_str() );
00057 if ( startAt == fileName )
00058 startAt = ":import";
00059 } else
00060 startAt = ":import";
00061
00062 QStringList fileNames =
00063 KFileDialog::getOpenFileNames( startAt,
00064 i18n( "*.mm *.mmf|Mealmaster (*.mm *.mmf)\n"
00065 "*|All Files (*)" ),
00066 this,
00067 i18n( "Import Mealmaster Files" ) );
00068
00069 if ( !fileNames.empty() ) {
00070 for ( int i=0; i<(signed)fileNames.size(); i++ )
00071 new KListViewItem( mealMasterListView, fileNames[ i ] );
00072 startTimer( 0 );
00073 updateEnableState();
00074 };
00075 }
00076
00077
00078 void ImportMealMasterDialog::timerEvent( QTimerEvent * )
00079 {
00080 assert( magic != NULL );
00081 killTimers();
00082
00083 for ( QListViewItem *item = mealMasterListView->firstChild();
00084 item != NULL; item = item->nextSibling() ) {
00085
00086 if ( item->text( 1 ).isEmpty() ) {
00087
00088
00089 const char *descr = magic_file( magic, item->text( 0 ) );
00090 if ( descr == NULL ) descr = magic_error( magic );
00091
00092 if ( descr == NULL )
00093 item->setText
00094 ( 1, i18n( "Error retrieving error-message from libmagic." ) );
00095 else
00096 item->setText( 1, descr );
00097
00098 startTimer( 0 );
00099 break;
00100
00101 };
00102
00103 };
00104
00105 }
00106
00107
00108 void ImportMealMasterDialog::updateEnableState()
00109 {
00110 bool okButtonEnable =
00111 mealMasterListView->firstChild() != NULL &&
00112 ( !storeErroneousRadio->isChecked() ||
00113 !errorFileNameEdit->text().isEmpty() ) &&
00114 encodingCombo->currentItem() != 0;
00115 okButton->setEnabled( okButtonEnable );
00116 }
00117
00118
00119 void ImportMealMasterDialog::selectionChanged()
00120 {
00121 bool removeButtonEnable = false;
00122 for ( QListViewItem *item = mealMasterListView->firstChild();
00123 item != NULL; item = item->nextSibling() )
00124 if ( item->isSelected() ) {
00125 removeButtonEnable = true;
00126 break;
00127 };
00128 removeButton->setEnabled( removeButtonEnable );
00129 }
00130
00131
00132 void ImportMealMasterDialog::removeSelected()
00133 {
00134 QListViewItem *item = mealMasterListView->firstChild();
00135 while ( item != NULL ) {
00136 QListViewItem *nextItem = item->nextSibling();
00137 if ( item->isSelected() )
00138 delete item;
00139 item = nextItem;
00140 };
00141 removeButton->setEnabled( false );
00142 updateEnableState();
00143 }
00144
00145
00146
00147
00148 void ImportMealMasterDialog::accept()
00149 {
00150 try {
00151
00152 assert( cookBook );
00153
00154 DisplayWaitCursor w;
00155
00156 boost::shared_ptr< std::ostream > stream;
00157 MealMasterParseErrorHandlerPtr parseErrorHandler;
00158
00159 if ( storeErroneousRadio->isChecked() ) {
00160
00161 stream = boost::shared_ptr< std::ostream >
00162 ( new std::ofstream( errorFileNameEdit->text(),
00163 std::ios::binary ) );
00164 parseErrorHandler = MealMasterParseErrorHandlerPtr
00165 ( new MealMasterStoreErroneousHandler
00166 ( stream.get(), errorFileNameEdit->text() ) );
00167
00168 } else if ( abortOnErrorRadio->isChecked() )
00169 parseErrorHandler = MealMasterParseErrorHandlerPtr( new MealMasterParseErrorHandler );
00170 else
00171 parseErrorHandler = MealMasterParseErrorHandlerPtr( new MealMasterIgnoreErrorHandler );
00172
00173
00174 std::list< int > fileSize;
00175 int numCharacters = 0;
00176 for ( QListViewItem *item = mealMasterListView->firstChild();
00177 item != NULL; item = item->nextSibling() ) {
00178 std::string fileName( (const char *)item->text( 0 ) );
00179 std::ifstream inputStream( fileName.c_str(), std::ios::binary );
00180 ERRORMACRO( inputStream.seekg( 0, std::ios::end ), Error, ,
00181 i18n( "Error opening file \"%1\" ..." ).
00182 arg( fileName.c_str() ) );
00183 fileSize.push_back( inputStream.tellg() );
00184 numCharacters += inputStream.tellg();
00185 };
00186
00187
00188 QProgressDialog progress( i18n( "Importing Mealmaster files ... " ),
00189 i18n( "&Cancel" ), numCharacters, this,
00190 "progress", true );
00191
00192
00193 int currPos = 0;
00194
00195
00196 for ( QListViewItem *item = mealMasterListView->firstChild();
00197 item != NULL; item = item->nextSibling() ) {
00198
00199 const int count = 50;
00200
00201
00202 CompilerPtr mealMasterCompiler
00203 ( new MealMasterCompiler( count, parseErrorHandler ) );
00204
00205
00206 std::string fileName( (const char *)item->text( 0 ) );
00207 std::ifstream inputFile( fileName.c_str(), std::ios::binary );
00208
00209 std::istream *inputStream;
00210 boost::shared_ptr< std::stringstream > stringStream;
00211
00212
00213 assert( encodingCombo->count() == 3 );
00214 assert( encodingCombo->currentItem() != 0 );
00215 assert( encodingCombo->text( 1 ) == i18n( "ISO-8859-1" ) );
00216 assert( encodingCombo->text( 2 ) == i18n( "UTF-8" ) );
00217
00218 if ( encodingCombo->currentItem() == 1 ) {
00219
00220 progress.setLabelText( i18n( "Recoding file \"%1\" ..." ).
00221 arg( fileName.c_str() ) );
00222 stringStream =
00223 boost::shared_ptr< std::stringstream >( new std::stringstream );
00224 inputStream = stringStream.get();
00225 Recoder( "latin1..utf8" ).translate( inputFile, *stringStream );
00226
00227 } else {
00228 assert( encodingCombo->currentItem() == 2 );
00229 inputStream = &inputFile;
00230 };
00231
00232
00233 progress.setLabelText( i18n( "Importing file \"%1\" ..." ).
00234 arg( fileName.c_str() ) );
00235 parseErrorHandler->setContext( i18n( "While importing file \"%1\"" ).
00236 arg( fileName.c_str() ).utf8().data() );
00237
00238 while ( !inputStream->eof() ) {
00239
00240
00241 progress.setProgress( currPos + inputStream->tellg() );
00242 ERRORMACRO( !progress.wasCanceled(), Error, ,
00243 i18n( "Import aborted." ) );
00244
00245
00246 std::ofstream nullDevice( "/dev/null" );
00247 ChainedCompiler
00248 ( mealMasterCompiler,
00249 cookBook->getXMLLayer() ).translate( *inputStream, nullDevice );
00250
00251 };
00252
00253
00254 currPos += fileSize.front();
00255 fileSize.pop_front();
00256
00257 };
00258
00259 QDialog::accept();
00260
00261 } catch ( Error &e ) {
00262
00263 KMessageBox::error( this, e.what() );
00264 QDialog::reject();
00265
00266 };
00267 }
00268