void InitBinDir(const std::string& argv0) { bool problem = false; try { // get this executable's path by following link const size_t BUF_SIZE = 2048; char buf[BUF_SIZE] = {0}; #ifdef __FreeBSD__ int mib[4]; mib[0] = CTL_KERN; mib[1] = KERN_PROC; mib[2] = KERN_PROC_PATHNAME; mib[3] = -1; size_t buf_size = sizeof(buf); sysctl(mib, 4, buf, &buf_size, 0, 0); #else size_t exe_path_size = readlink("/proc/self/exe", buf, BUF_SIZE); if (exe_path_size == static_cast<size_t>(-1)) { problem = true; } else { if (exe_path_size >= BUF_SIZE || exe_path_size < 0) exe_path_size = BUF_SIZE - 1; // ensure buffer isn't accessed out of range buf[exe_path_size] = 0; // null terminate c-string } #endif if (!problem) { buf[BUF_SIZE - 1] = 0; // to be safe, else initializing an std::string with a non-null-terminated string could read invalid data outside the buffer range std::string path_text(buf); fs::path binary_file = fs::system_complete(fs::path(path_text)); bin_dir = binary_file.branch_path(); // check that a "freeorion" file (hopefully the freeorion binary) exists in the found directory fs::path p(bin_dir); p /= "freeorion"; if (!exists(p)) problem = true; } } catch (fs::filesystem_error err) { problem = true; } if (problem) { // failed trying to parse the call path, so try hard-coded standard location... char* dir_name = br_find_bin_dir("/usr/local/bin"); fs::path p(dir_name); std::free(dir_name); // if the path does not exist, fall back to the working directory if (!exists(p)) { bin_dir = fs::initial_path(); } else { bin_dir = p; } } }
/*! * \en * Shows report. * \_en * \ru * Показывает отчет. * \_ru */ void aReport::show() { if(type==RT_text) { browser->textBrowser->setText( tpl->result() ); browser->show(); } if(type==RT_office_writer || type==RT_office_calc) { QString fileName = getName4NewTemplate(); tpl->cleanUpTags(); tpl->save(fileName); BrInitError error; if (br_init_lib(&error) == 0 && error != BR_INIT_ERROR_DISABLED) { aLog::print(aLog::MT_INFO, tr("Warning: BinReloc failed to initialize (error code %1)\n").arg(error)); aLog::print(aLog::MT_INFO, tr("Will fallback to hardcoded default path.\n")); } aLog::print(aLog::MT_DEBUG, tr("aReport::show BinReloc path to bin dir is %1.\n").arg( br_find_bin_dir("/usr/bin") )); QString startCatalog = br_find_bin_dir("/usr/bin"); QString filter; bool sofficeFound = false; bool ok; QString oowriter = aService::readConfigVariable("OpenOfficeExecutable", &ok); if(!ok || oowriter == "" ) { #ifdef Q_OS_WIN32 aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable not found in registery")); startCatalog = QString("%1\\Program Files\\OpenOffice.org1.1.1\\program").arg(getenv("HOMEDRIVE")); QFile soffice(startCatalog + "\\soffice.exe"); if(!soffice.exists()) { startCatalog = QString("%1\\Program Files\\OpenOffice.org 2.0\\program").arg(getenv("HOMEDRIVE")); soffice.setName(startCatalog + "\\soffice.exe"); if(!soffice.exists()) { startCatalog = QString("%1\\Program Files").arg(getenv("HOMEDRIVE")); } else { aService::writeConfigVariable("OpenOfficeExecutable",startCatalog + "\\soffice.exe"); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable found %1").arg(startCatalog + "\\soffice.exe")); } } else { aService::writeConfigVariable("OpenOfficeExecutable",startCatalog + "\\soffice.exe"); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable found %1").arg(startCatalog + "\\soffice.exe")); } filter = "Executable files (*.exe)"; #else aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable not found in ~/.ananas/configrc")); QFile soffice(startCatalog + "/ooffice"); if(!soffice.exists()) { soffice.setName(startCatalog + "/ooffice2"); if(soffice.exists()) { aService::writeConfigVariable("OpenOfficeExecutable",startCatalog + "/ooffice2"); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable found %1").arg(startCatalog + "/ooffice2")); } } else { aService::writeConfigVariable("OpenOfficeExecutable",startCatalog + "/ooffice"); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable found %1").arg(startCatalog + "/ooffice")); } #endif } oowriter = aService::readConfigVariable("OpenOfficeExecutable", &ok); if(!ok || oowriter == "" ) { QFileDialog dlg(0,"select_file_dialog",true); dlg.addFilter( filter ); dlg.setMode(QFileDialog::ExistingFile); dlg.setDir(QDir(startCatalog)); dlg.setCaption("Для отображения отчета необходим OpenOffice. Укажите исполняемый файл OpenOffice"); if(dlg.exec()==QDialog::Accepted) { oowriter = dlg.selectedFile(); //printf("select %s", oowriter.ascii()); QProcess process( oowriter ); process.addArgument( "-n" ); process.addArgument( QDir::convertSeparators( fileName ) ); if( !process.start() ) { QMessageBox::warning(0, tr("Warning"), tr("Unable to start OpenOffice (%1)").arg(oowriter), QMessageBox::Ok,QMessageBox::NoButton); // printf("Unable to start OpenOffice Writer\n"); } else { aService::writeConfigVariable("OpenOfficeExecutable",oowriter); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable set to %1").arg(oowriter)); } } } else { QProcess process( oowriter ); process.addArgument( "-n" ); process.addArgument( QDir::convertSeparators( fileName ) ); if( !process.start() ) { QMessageBox::warning(0, tr("Warning"), tr("Unable to start OpenOffice (%1)").arg(oowriter), QMessageBox::Ok,QMessageBox::NoButton); // printf("Unable to start OpenOffice Writer\n"); } else { aService::writeConfigVariable("OpenOfficeExecutable",oowriter); aLog::print(aLog::MT_INFO,tr("aReport OpenOfficeExecutable set to %1").arg(oowriter)); } } //tpl->open( tplName ); } if (type==RT_msoffice_word || type==RT_msoffice_excel) { QString fileName = getName4NewTemplate(); tpl->cleanUpTags(); tpl->save(fileName); QString msoCatalog = QString("%1\\Program Files\\Microsoft Office\\OFFICE11").arg(getenv("HOMEDRIVE")); QString configItem, msoAppName, msoAppFileName; if (type == RT_msoffice_word) { configItem = "MSOfficeWordExecutable"; msoAppName = "Word"; msoAppFileName = "winword.exe"; } else { configItem = "MSOfficeExcelExecutable"; msoAppName = "Excel"; msoAppFileName = "excel.exe"; } //реестр bool ok; QString msoReport = aService::readConfigVariable(configItem, &ok); //в реестре пусто if (!ok || msoReport.isEmpty()) { aLog::print(aLog::MT_INFO,tr("aReport "+configItem+" not found in registery")); QFile msoReportExe(msoCatalog + "\\"+msoAppFileName); //если файла нету if(!msoReportExe.exists()) { QFileDialog dlg(0,"select_file_dialog",true); QString filter; filter = "Executable files (*.exe)"; dlg.addFilter( filter ); dlg.setMode(QFileDialog::ExistingFile); dlg.setDir(QDir(msoCatalog)); dlg.setCaption(tr("For display of the report it is necessary Microsoft Office "+msoAppName+" 2003. Specify executed file Microsoft Office "+msoAppName+" 2003")); if(dlg.exec()==QDialog::Accepted) { msoReport = dlg.selectedFile(); } } //сохраним в реестр else { //aService::writeConfigVariable("MSOfficeWordExecutable",winwordexe.name()); msoReport = msoReportExe.name(); aLog::print(aLog::MT_INFO,tr("aReport "+configItem+" found %1").arg(msoReportExe.name())); } QProcess process( msoReport ); process.addArgument( QDir::convertSeparators( fileName ) ); if( !process.start() ) { if (!msoReport.isEmpty()) QMessageBox::warning(0, tr("Warning"), tr("Unable to start Microsoft Office "+msoAppName+" 2003 (%1)").arg(msoReport), QMessageBox::Ok,QMessageBox::NoButton); else QMessageBox::warning(0, tr("Warning"), tr("Unable to start Microsoft Office "+msoAppName+" 2003"), QMessageBox::Ok,QMessageBox::NoButton); } else { aService::writeConfigVariable(configItem,msoReport); aLog::print(aLog::MT_INFO,tr("aReport "+configItem+" set to %1").arg(msoReport)); } } //тогда запускаем else { QProcess process( msoReport ); process.addArgument( QDir::convertSeparators( fileName ) ); if( !process.start() ) { QMessageBox::warning(0, tr("Warning"), tr("Unable to start Microsoft Office "+msoAppName+" 2003 (%1)").arg(msoReport), QMessageBox::Ok,QMessageBox::NoButton); } else { aService::writeConfigVariable(configItem,msoReport); aLog::print(aLog::MT_INFO,tr("aReport "+configItem+" set to %1").arg(msoReport)); } } } }