Main | License | Installation | FAQ | Screenshots | Contact | Links | Sf.net | Freshmeat.net | KDE-Apps.org
Requirements | Design | Modules | Class Hierarchy | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

importMealMasterDialog.ui.h

Go to the documentation of this file.
00001 /* Copyright (C) 2002, 2003, 2004 Jan Wedekind.
00002    This file is part of the recipe database application AnyMeal.
00003 
00004    AnyMeal is free software; you can redistribute it and/or modify it under
00005    the terms of the GNU GENERAL PUBLIC LICENSE as published by the Free
00006    Software Foundation; either version 3 of the License, or (at your option)
00007    any later version.
00008 
00009    AnyMeal is distributed in the hope that it will be useful, but WITHOUT ANY
00010    WARRANTY; without even the implied warranty of MERCHANTIBILITY or FITNESS
00011    FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00012    details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
00016 /****************************************************************************
00017  ** ui.h extension file, included from the uic-generated form implementation.
00018  **
00019  ** If you wish to add, delete or rename slots use Qt Designer which will
00020  ** update this file, preserving your code. Create an init() slot in place of
00021  ** a constructor, and a destroy() slot in place of a destructor.
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   // Select file for saving erroneous Mealmaster recipes to.
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       // DisplayWaitCursor w;
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     // Compute overall number of characters to be parsed.
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     // Open progress dialog.
00188     QProgressDialog progress( i18n( "Importing Mealmaster files ... " ),
00189                               i18n( "&Cancel" ), numCharacters, this,
00190                               "progress", true );
00191 
00192     // Overall progress is zero.
00193     int currPos = 0;
00194 
00195     // Start reading in files.
00196     for ( QListViewItem *item = mealMasterListView->firstChild();
00197           item != NULL; item = item->nextSibling() ) {
00198 
00199       const int count = 50;
00200       // Start parsing new file in steps of 'count' recipes.
00201       // Constructor reset's counter for line-number!
00202       CompilerPtr mealMasterCompiler
00203         ( new MealMasterCompiler( count, parseErrorHandler ) );
00204 
00205       // Open the next file.
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       // Recode file.
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       // Display info.
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         // Update overall progress.
00241         progress.setProgress( currPos + inputStream->tellg() );
00242         ERRORMACRO( !progress.wasCanceled(), Error, ,
00243                     i18n( "Import aborted." ) );
00244 
00245         // Parse a couple of recipes.
00246         std::ofstream nullDevice( "/dev/null" );
00247         ChainedCompiler
00248           ( mealMasterCompiler,
00249             cookBook->getXMLLayer() ).translate( *inputStream, nullDevice );
00250 
00251       };
00252 
00253       // Increment offset of progress.
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 


anymeal 0.30 - recipe management software - Make the most of your food! - © Jan Wedekind Sun Sep 14 16:01:26 2008 - GNU Free Documentation License