#include #include #include #include "mainwindow.h" #include "ui_mainwindow.h" #include "pbi.h" PBI_Struct *pbi_data = NULL; QString PROG_DIR = QDir::homePath() + "/EasyPBI"; QString PORTS_DIR = ""; QString PORT_SELECTED = ""; MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { ui->setupUi(this); ui->progressBar->setVisible(FALSE); } MainWindow::~MainWindow() { if (pbi_data) { Clean_Up(pbi_data, TRUE); } //delete ui; } void MainWindow::on_actionExit_triggered() { close(); } void MainWindow::on_actionReset_2_triggered() { Reset_Form(); } void MainWindow::Reset_Form() { if (pbi_data) { Clean_Up(pbi_data, TRUE); pbi_data = NULL; } ui->port_name->setText(""); ui->port_category->setText(""); ui->author->setText(""); ui->icon_path->setText(""); ui->gui_app->setChecked(true); ui->website->setText(""); } void MainWindow::on_pushButton_2_clicked() { QString icon_name; icon_name = QFileDialog::getOpenFileName(this, tr("Select Icon"), "~", tr("Image Files (*.png)")); ui->icon_path->setText(icon_name); } void MainWindow::on_actionAbout_EasyPBI_triggered() { QMessageBox::about(this, "EasyPBI", "EasyPBI 0.1\nCreate PBI modules quickly and easily.\nhttp://makeapbi.sourceforge.net\nWritten by Jesse Smith"); } void MainWindow::on_actionNew_triggered() { QString module_path; int status; char *argv[] = {"-9", "-s", ""}; // clear out any existing module if (pbi_data) { Reset_Form(); } // check to see if the ports tree exists status = Check_For_Ports(); if (! status) { QMessageBox::about(this, "Error", "We could not locate the ports tree on your system.\nPlease install the FreeBSD system ports tree as root, or select 'Get Ports' from the File menu to retrieve the ports tree for this user only.\n"); return; } // get new port to work on module_path = QFileDialog::getExistingDirectory(this, "Select Port", PORTS_DIR); if(module_path.isEmpty()){return;} //action cancelled or closed PORT_SELECTED = module_path.section("ports/",1,1); pbi_data = Init_Data(); if (! pbi_data) { QMessageBox::about(this, "Error", "Could not set up memory for this module."); } // get the name, arguments argv[2] = strdup(qPrintable(module_path)); Get_Options(pbi_data, 3, argv); // confirm port exists status = Port_Exists(pbi_data); if (! status) { QMessageBox::about(this, "Error", "This port does not exist."); Clean_Up(pbi_data, TRUE); return; } Get_Name(pbi_data); Get_Icon(pbi_data); Get_Category(pbi_data); Get_Website(pbi_data); Get_Author(pbi_data); ui->author->setText(pbi_data->author_name); ui->port_category->setText(pbi_data->category); if (pbi_data->icon_name) ui->icon_path->setText(pbi_data->icon_name); ui->port_name->setText(pbi_data->port_name); ui->website->setText(pbi_data->web_site); ui->status_bar->setText("Please fill out the above fields and click Create Module."); } void MainWindow::on_gui_app_clicked() { if (ui->gui_app->isChecked()) pbi_data->console_app = FALSE; else pbi_data->console_app = TRUE; } void MainWindow::on_create_button_clicked() { char *home_dir; char final_output[1024]; BIN_FILE *current_exe; //int status; FILE *my_file; if (! pbi_data) { QMessageBox::about(this, "Error", "Please create a new module from the File menu first."); return; } // find where we want to save the module ui->status_bar->setText("Please wait while we build the module..."); // save text fields to data structure if (pbi_data->author_name) free(pbi_data->author_name); if (pbi_data->category) free(pbi_data->category); if (pbi_data->icon_name) free(pbi_data->icon_name); if (pbi_data->web_site) free(pbi_data->web_site); pbi_data->author_name = strdup( qPrintable(ui->author->text())); pbi_data->category = strdup( qPrintable(ui->port_category->text())); pbi_data->icon_name = strdup( qPrintable(ui->icon_path->text())); pbi_data->web_site = strdup( qPrintable(ui->website->text())); // chdir to save location home_dir = getenv("HOME"); chdir(home_dir); chdir("EasyPBI"); //Already exists (checked before now) mkdir("Modules", 0777); // build module Create_Module_Template9(pbi_data); Get_Main_Exe_Smart(pbi_data); Get_More_Exe(pbi_data); Copy_Icon9(pbi_data); Write_Conf9(pbi_data); Write_Menu(pbi_data, NULL); current_exe = pbi_data->more_exe; while (current_exe) { Write_Menu(pbi_data, current_exe->path); current_exe = (BIN_FILE *) current_exe->next; } // create tarball of module sprintf(final_output, "tar czf Modules/%s.tar.gz %s", pbi_data->port_name9, pbi_data->port_name9); system(final_output); sprintf(final_output, "Modules/%s.tar.gz", pbi_data->port_name9); my_file = fopen(final_output, "r"); if (my_file) { sprintf(final_output, "Done. Your module is located at %s/EasyPBI/Modules/\nPlease e-mail the file '%s.tar.gz' to the PBI team at: pbi-dev@lists.pcbsd.org", home_dir, pbi_data->port_name9); fclose(my_file); } else sprintf(final_output, "Something went wrong creating the module. There may be a permission error creating new directories in your home."); ui->status_bar->setText(final_output); } void MainWindow::on_port_info_button_clicked() { //Check if a port is selected if(! pbi_data){ QMessageBox::warning(this, "Error: Port Information", "Please select a port first"); return; } //Open up the freshports.org page for the selected port QString target_url = "http://freshports.org/" + PORT_SELECTED; QDesktopServices::openUrl(QUrl(target_url)); } int MainWindow::Check_For_Ports() { //Check that the program Directory exists first if( !QDir(PROG_DIR).exists() ){ QDir dir; dir.root(); if( !dir.mkdir(PROG_DIR) ){ //Create the program directory //Could not create the Program directory QMessageBox::warning(this, "Error: Permissions", "Invalid permissions to create EasyPBI directory in home folder"); return FALSE; } } QStringList portsLocations; //Set the locations to search for the ports tree portsLocations << "/usr/ports" << PROG_DIR +"/ports"; //Search the locations for(int i=0; iget(QNetworkRequest(QUrl("ftp://ftp.freebsd.org/pub/FreeBSD/ports/ports/ports.tar.gz"))); ui->status_bar->setText("Downloading the FreeBSD ports tree: Please Wait."); //Make the Progress Bar visible and start updating ui->progressBar->setValue(0); ui->progressBar->setVisible(TRUE); connect(currentDL, SIGNAL(downloadProgress(qint64,qint64)), this, SLOT(updateProgress(qint64,qint64)) ); } void MainWindow::downloadFinished(QNetworkReply *reply){ //Disable the progress bar ui->progressBar->setVisible(FALSE); //See if the Download was successful if( reply->error() ){ QMessageBox::warning(this, "Download Failed","Downloading the FreeBSD ports tree failed. Please try again."); return; } //Download Succesful, save it to file QFile file(PROG_DIR+"/ports.tar.gz"); if( !file.open(QIODevice::WriteOnly)){ //if file could not be opened QMessageBox::warning(this, "Saving Failed","Saving the FreeBSD ports tree file failed. Please try again."); reply->deleteLater(); return; } file.write(reply->readAll()); file.close(); //Saving the file successful, delete the downloaded data from memory reply->deleteLater(); ui->status_bar->setText("Download finished. Extracting the ports tree into the EasyPBI directory."); QTimer::singleShot(10,this, SLOT(extractPT()) ); return; } void MainWindow::extractPT(){ //Unpack the file QString cmd = "cd " + PROG_DIR + "; tar xvf ports.tar.gz"; system(cmd.toLatin1()); //Delete the file QFile file(PROG_DIR+"/ports.tar.gz"); file.remove(); //Let the user know that the ports tree was successfully added ui->status_bar->setText("The FreeBSD ports tree has been successfully added to the EasyPBI directory.\nPlease select 'New Module' from the file menu to continue."); return; } void MainWindow::updateProgress(qint64 bytesReceived, qint64 bytesTotal){ //Determine the percentage int percent = (bytesReceived*100)/bytesTotal; //Update the progress bar ui->progressBar->setValue(percent); ui->progressBar->update(); }