void OGRGDALImportExtension::processButtonBoxClicked(QAbstractButton* Button) { QDialogButtonBox::StandardButton StdButton = ui->ButtonBox->standardButton(Button); if (StdButton == QDialogButtonBox::Cancel) cancelImport(); else if(StdButton == QDialogButtonBox::Apply) proceedToImport(); }
ImportFromGG::ImportFromGG(const Account &acc, QString file, QObject *p): Importer(acc, p) { //sprawdź czy plik istnieje QFile efile(file); if (!efile.exists()) { QMessageBox::critical(0, tr("Error"), tr("File does no exist.")); cancelImport(); return; } if (account.isNull() || !History::instance()->currentStorage()) { QMessageBox::critical(0, tr("Error"), tr("Could not find any Gadu-Gadu account.")); cancelImport(); return; } //wstępnie przeanalizuj plik z historią arch=new MemFile(file); //plik z archiwum - do odczytu danych arch->open(QIODevice::ReadOnly); /* odczytaj nagłówek pliku */ arch->read(reinterpret_cast<char*>(&gg_header),sizeof(gg_header)); owner_uin=gg_header.uin ^ 0xFFFFFD66; /* sprawdź czy uin sie zgadza */ unsigned int acc_uin=account.id().toInt(); if (acc_uin!=owner_uin) { if (QMessageBox::Yes==QMessageBox::warning(0, tr("Warning"), tr("It seems that it is not your Gadu-Gadu archive.\n" "The uin saved in archives.dat file, and your current uin are different.\n" "Cancel import?"), QMessageBox::Yes|QMessageBox::No,QMessageBox::Yes) ) cancelImport(); } }
bool QgsShapeFile::insertLayer(QString dbname, QString schema, QString geom_col, QString srid, PGconn * conn, QProgressDialog * pro, bool &fin){ connect(pro, SIGNAL(cancelled()), this, SLOT(cancelImport())); import_cancelled = false; bool result = true; QString query = "CREATE TABLE "+schema+"."+table_name+"(gid int4 PRIMARY KEY, "; for(int n=0; n<column_names.size() && result; n++){ if(!column_names[n][0].isLetter()) result = false; char * esc_str = new char[column_names[n].length()*2+1]; PQescapeString(esc_str, (const char *)column_names[n].lower(), column_names[n].length()); query += esc_str; query += " "; query += column_types[n]; if(n<column_names.size()-1) query += ", "; delete[] esc_str; } query += " )"; PGresult *res = PQexec(conn, (const char *)query); qWarning(query); if(PQresultStatus(res)!=PGRES_COMMAND_OK){ // flag error and send query and error message to stdout on debug result = false; qWarning(PQresultErrorMessage(res)); } else { PQclear(res); } query = "SELECT AddGeometryColumn(\'" + dbname + "\', \'" + table_name + "\', \'"+geom_col+"\', " + srid + ", \'" + geom_type + "\', 2)"; if(result) res = PQexec(conn, (const char *)query); if(PQresultStatus(res)!=PGRES_TUPLES_OK){ result = false; } else{ qWarning(query); qWarning(PQresultErrorMessage(res)); PQclear(res); } //adding the data into the table for(int m=0;m<features && result; m++){ if(import_cancelled){ fin = true; break; } OGRFeature *feat = ogrLayer->GetNextFeature(); if(feat){ OGRGeometry *geom = feat->GetGeometryRef(); if(geom){ query = "INSERT INTO "+schema+"."+table_name+QString(" VALUES( %1, ").arg(m); int num = geom->WkbSize(); char * geo_temp = new char[num*3]; geom->exportToWkt(&geo_temp); QString geometry(geo_temp); QString quotes; for(int n=0; n<column_types.size(); n++){ if(column_types[n] == "int" || column_types[n] == "float") quotes = " "; else quotes = "\'"; query += quotes; // escape the string value QString val = feat->GetFieldAsString(n); char * esc_str = new char[val.length()*2+1]; PQescapeString(esc_str, (const char *)val.lower(), val.length()); // add escaped value to the query query += esc_str; query += QString(quotes + ", "); delete[] esc_str; } query += QString("GeometryFromText(\'")+geometry+QString("\', ")+srid+QString("))"); if(result) res = PQexec(conn, (const char *)query); if(PQresultStatus(res)!=PGRES_COMMAND_OK){ // flag error and send query and error message to stdout on debug result = false; qWarning(PQresultErrorMessage(res)); } else { PQclear(res); } pro->setProgress(pro->progress()+1); qApp->processEvents(); delete[] geo_temp; } delete feat; } } ogrLayer->ResetReading(); return result; }
bool QgsShapeFile::insertLayer( QString dbname, QString schema, QString primary_key, QString geom_col, QString srid, PGconn * conn, QProgressDialog& pro, bool &fin, QString& errorText ) { connect( &pro, SIGNAL( canceled() ), this, SLOT( cancelImport() ) ); import_canceled = false; bool result = true; QString query = QString( "CREATE TABLE %1.%2(%3 int4 PRIMARY KEY" ) .arg( QgsPgUtil::quotedIdentifier( schema ) ) .arg( QgsPgUtil::quotedIdentifier( table_name ) ) .arg( QgsPgUtil::quotedIdentifier( primary_key ) ); for ( uint n = 0; n < column_names.size() && result; n++ ) { query += QString( ",%1 %2" ) .arg( QgsPgUtil::quotedIdentifier( column_names[n] ) ) .arg( column_types[n] ); } query += " )"; QgsDebugMsg( "Query string is: " + query ); PGresult *res = PQexec( conn, query.toUtf8() ); if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) { // flag error and send query and error message to stdout on debug errorText += tr( "The database gave an error while executing this SQL:" ) + "\n"; errorText += query + '\n'; errorText += tr( "The error was:" ) + "\n"; errorText += PQresultErrorMessage( res ) + '\n'; PQclear( res ); return false; } else { PQclear( res ); } query = QString( "SELECT AddGeometryColumn(%1,%2,%3,%4,%5,2)" ) .arg( QgsPgUtil::quotedValue( schema ) ) .arg( QgsPgUtil::quotedValue( table_name ) ) .arg( QgsPgUtil::quotedValue( geom_col ) ) .arg( srid ) .arg( QgsPgUtil::quotedValue( geom_type ) ); res = PQexec( conn, query.toUtf8() ); if ( PQresultStatus( res ) != PGRES_TUPLES_OK ) { errorText += tr( "The database gave an error while executing this SQL:" ) + "\n"; errorText += query + '\n'; errorText += tr( "The error was:" ) + "\n"; errorText += PQresultErrorMessage( res ) + '\n'; PQclear( res ); return false; } else { PQclear( res ); } if ( isMulti ) { query = QString( "select constraint_name from information_schema.table_constraints where table_schema=%1 and table_name=%2 and constraint_name in ('$2','enforce_geotype_the_geom')" ) .arg( QgsPgUtil::quotedValue( schema ) ) .arg( QgsPgUtil::quotedValue( table_name ) ); QStringList constraints; res = PQexec( conn, query.toUtf8() ); if ( PQresultStatus( res ) == PGRES_TUPLES_OK ) { for ( int i = 0; i < PQntuples( res ); i++ ) constraints.append( PQgetvalue( res, i, 0 ) ); } PQclear( res ); if ( constraints.size() > 0 ) { // drop the check constraint // TODO This whole concept needs to be changed to either // convert the geometries to the same type or allow // multiple types in the check constraint. For now, we // just drop the constraint... query = QString( "alter table %1 drop constraint %2" ) .arg( QgsPgUtil::quotedIdentifier( table_name ) ) .arg( QgsPgUtil::quotedIdentifier( constraints[0] ) ); res = PQexec( conn, query.toUtf8() ); if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) { errorText += tr( "The database gave an error while executing this SQL:" ) + "\n"; errorText += query + '\n'; errorText += tr( "The error was:" ) + "\n"; errorText += PQresultErrorMessage( res ) + '\n'; PQclear( res ); return false; } PQclear( res ); } } //adding the data into the table for ( int m = 0; m < features && result; m++ ) { if ( import_canceled ) { fin = true; break; } OGRFeatureH feat = OGR_L_GetNextFeature( ogrLayer ); if ( feat ) { OGRGeometryH geom = OGR_F_GetGeometryRef( feat ); if ( geom ) { query = QString( "INSERT INTO %1.%2 VALUES(%3" ) .arg( QgsPgUtil::quotedIdentifier( schema ) ) .arg( QgsPgUtil::quotedIdentifier( table_name ) ) .arg( m ); char *geo_temp; // 'GeometryFromText' supports only 2D coordinates // TODO for proper 2.5D support we would need to use 'GeomFromEWkt' if ( hasMoreDimensions ) OGR_G_SetCoordinateDimension( geom, 2 ); OGR_G_ExportToWkt( geom, &geo_temp ); QString geometry( geo_temp ); CPLFree( geo_temp ); for ( uint n = 0; n < column_types.size(); n++ ) { QString val; // FIXME: OGR_F_GetFieldAsString returns junk when called with a 8.255 float field if ( column_types[n] == "float" ) val = QString::number( OGR_F_GetFieldAsDouble( feat, n ) ); else val = codec->toUnicode( OGR_F_GetFieldAsString( feat, n ) ); if ( val.isEmpty() ) val = "NULL"; else val = QgsPgUtil::quotedValue( val ); query += "," + val; } query += QString( ",GeometryFromText(%1,%2))" ) .arg( QgsPgUtil::quotedValue( geometry ) ) .arg( srid ); if ( result ) res = PQexec( conn, query.toUtf8() ); if ( PQresultStatus( res ) != PGRES_COMMAND_OK ) { // flag error and send query and error message to stdout on debug result = false; errorText += tr( "The database gave an error while executing this SQL:" ) + "\n"; // the query string can be quite long. Trim if necessary... if ( query.count() > 100 ) errorText += query.left( 150 ) + tr( "... (rest of SQL trimmed)", "is appended to a truncated SQL statement" ) + "\n"; else errorText += query + '\n'; errorText += tr( "The error was:" ) + "\n"; errorText += PQresultErrorMessage( res ); errorText += '\n'; } else { PQclear( res ); } pro.setValue( pro.value() + 1 ); qApp->processEvents(); } OGR_F_Destroy( feat ); } } // create the GIST index if the the load was successful if ( result ) { // prompt user to see if they want to build the index and warn // them about the potential time-cost } OGR_L_ResetReading( ogrLayer ); return result; }
void OGRGDALImportExtension::closeEvent(QCloseEvent* Event) { Event->ignore(); cancelImport(); }