00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 void ImportRecipeMLDialog::openRecipeMLFile()
00015 {
00016 std::string startAt;
00017
00018
00019
00020
00021
00022
00023 startAt = ":import";
00024
00025 QStringList fileNames =
00026 KFileDialog::getOpenFileNames( startAt,
00027 i18n( "*.recipeml *.xml|RecipeML (*.recipeml *.xml)\n"
00028 "*|All Files (*)" ),
00029 this,
00030 i18n( "Import RecipeML Files" ) );
00031
00032 if ( !fileNames.empty() ) {
00033 for ( int i=0; i<(signed)fileNames.size(); i++ )
00034 recipeMLListBox->insertItem( fileNames[i] );
00035 updateEnableState();
00036 };
00037
00038 }
00039
00040
00041 void ImportRecipeMLDialog::openRecipeMLDir()
00042 {
00043 QString folder =
00044 KFileDialog::getExistingDirectory( ":import",
00045 this,
00046 i18n( "Import RecipeML folder" ) );
00047 if ( folder != QString::null ) {
00048 recipeMLListBox->insertItem( folder );
00049 updateEnableState();
00050 };
00051 }
00052
00053 void ImportRecipeMLDialog::openErrorDir()
00054 {
00055
00056 QString dirName =
00057 KFileDialog::getExistingDirectory( ":export",
00058 this,
00059 i18n( "Store Erroneous RecipeML Recipes" ) );
00060
00061 if ( dirName != QString::null ) {
00062 errorDirNameEdit->setText( dirName );
00063 updateEnableState();
00064 };
00065 }
00066
00067
00068 void ImportRecipeMLDialog::updateEnableState()
00069 {
00070 bool okButtonEnable =
00071 recipeMLListBox->firstItem() != NULL &&
00072 ( !storeErroneousRadio->isChecked() ||
00073 !errorDirNameEdit->text().isEmpty() );
00074 okButton->setEnabled( okButtonEnable );
00075 }
00076
00077
00078 void ImportRecipeMLDialog::selectionChanged()
00079 {
00080 bool removeButtonEnable = false;
00081 for ( QListBoxItem *item = recipeMLListBox->firstItem();
00082 item != NULL; item = item->next() )
00083 if ( item->isSelected() ) {
00084 removeButtonEnable = true;
00085 break;
00086 };
00087 removeButton->setEnabled( removeButtonEnable );
00088 }
00089
00090
00091 void ImportRecipeMLDialog::removeSelected()
00092 {
00093 QListBoxItem *item = recipeMLListBox->firstItem();
00094 while ( item != NULL ) {
00095 QListBoxItem *nextItem = item->next();
00096 if ( item->isSelected() )
00097 delete item;
00098 item = nextItem;
00099 };
00100 removeButton->setEnabled( false );
00101 updateEnableState();
00102 }
00103
00104
00105 void ImportRecipeMLDialog::accept()
00106 {
00107 DIR *dir = 0;
00108 try {
00109
00110 assert( cookBook );
00111
00112 DisplayWaitCursor w;
00113
00114
00115 std::list< std::string > fileNames;
00116 for ( int i=0; i<(signed)recipeMLListBox->count(); i++ ) {
00117 std::string name( recipeMLListBox->text( i ).utf8() );
00118 if ( testFile( name ) == EISDIR ) {
00119 dir = opendir( name.c_str() );
00120 ERRORMACRO( dir != 0, Error, ,
00121 "Error accessing directory \"" << name << "\": "
00122 << strerror( errno ) );
00123 struct dirent *entry = 0;
00124 errno = 0;
00125 while ( ( entry = readdir( dir ) ) != 0 ) {
00126 if ( testFile( name + "/" + entry->d_name ) != EISDIR )
00127 fileNames.push_back( name );
00128 errno = 0;
00129 };
00130 ERRORMACRO( errno == 0, Error, ,
00131 "Error reading entry from directory \"" << name << "\": "
00132 << strerror( errno ) );
00133 } else
00134 fileNames.push_back( name );
00135 };
00136
00137
00138 recipeMLToRecipe->setEmptyResult
00139 ( "<?xml version='1.0' encoding='UTF-8'?>\n"
00140 "<insert/>" );
00141
00142
00143 XSLParseErrorHandlerPtr parseErrorHandler;
00144 if ( storeErroneousRadio->isChecked() )
00145 parseErrorHandler = XSLParseErrorHandlerPtr( new XSLParseErrorHandler );
00146
00147 else if ( abortOnErrorRadio->isChecked() )
00148 parseErrorHandler = XSLParseErrorHandlerPtr( new XSLParseErrorHandler );
00149 else {
00150 assert( ignoreErrorsRadio->isChecked() );
00151 parseErrorHandler = XSLParseErrorHandlerPtr( new XSLIgnoreErrorHandler );
00152 };
00153 recipeMLToRecipe->setParseErrorHandler( parseErrorHandler );
00154
00155
00156 QProgressDialog progress( i18n( "Importing RecipeML files ... " ),
00157 i18n( "&Cancel" ), fileNames.size(), this,
00158 "progress", true );
00159
00160
00161 int c = 0;
00162 for ( std::list< std::string >::const_iterator i=fileNames.begin();
00163 i != fileNames.end(); i++ ) {
00164
00165
00166 progress.setProgress( c++ );
00167 ERRORMACRO( !progress.wasCanceled(), Error, ,
00168 i18n( "Import aborted." ) );
00169
00170 QString context = i18n( "Importing file \"%1\" ..." ).arg( i->c_str() );
00171 progress.setLabelText( context );
00172 parseErrorHandler->setContext( i18n( "While importing file \"%1\"" ).
00173 arg( i->c_str() ).utf8().data() );
00174
00175 std::ifstream inputFile( i->c_str(), std::ios::binary );
00176 std::ofstream nullDevice( "/dev/null" );
00177 ChainedCompiler
00178 ( recipeMLToRecipe,
00179 cookBook->getXMLLayer() ).translate( inputFile, nullDevice );
00180
00181 };
00182
00183 QDialog::accept();
00184
00185 } catch ( Error &e ) {
00186
00187 if ( dir != 0 ) closedir( dir );
00188 KMessageBox::error( this, e.what() );
00189 QDialog::reject();
00190
00191 };
00192
00193
00194 recipeMLToRecipe->setParseErrorHandler
00195 ( XSLParseErrorHandlerPtr( new XSLParseErrorHandler ) );
00196
00197 recipeMLToRecipe->setEmptyResult( "" );
00198 }