Example #1
0
/**
 * This function is called when leaving this wizard page.
 * Saves the made settings and checks some values.
 * @return   the success state
 */
bool CodeGenOptionsPage::save()
{
    // first save the settings to the selected generator policy
    apply();

    // before going on to the generation page, check that the output directory
    // exists and is writable

    // get the policy for the current code generator
    CodeGenerationPolicy *policy = UMLApp::app()->commonPolicy();

    // get the output directory path
    QFileInfo info(policy->getOutputDirectory().absolutePath());
    if (info.exists()) {
        // directory exists... make sure we can write to it
        if (!info.isWritable()) {
            KMessageBox::sorry(this, i18n("The output folder exists, but it is not writable.\nPlease set the appropriate permissions or choose another folder."),
                    i18n("Error Writing to Output Folder"));
            return false;
        }
        // it exits and we can write... make sure it is a directory
        if (!info.isDir()) {
            KMessageBox::sorry(this, i18n("%1 does not seem to be a folder. Please choose a valid folder.", info.filePath()),
                    i18n("Please Choose Valid Folder"));
            return false;
        }
    }
    else {
        if (KMessageBox::questionYesNo(this,
                        i18n("The folder %1 does not exist. Do you want to create it now?", info.filePath()),
                        i18n("Output Folder Does Not Exist"), KGuiItem(i18n("Create Folder")), KGuiItem(i18n("Do Not Create"))) == KMessageBox::Yes)
        {
            QDir dir;
            if (!dir.mkdir(info.filePath())) {
                KMessageBox::sorry(this, i18n("The folder could not be created.\nPlease make sure you have write access to its parent folder or select another, valid, folder."),
                            i18n("Error Creating Folder"));
                return false;
            }
            // else, directory created
        }
        else {  // do not create output directory
            KMessageBox::information(this, i18n("Please select a valid folder."),
                          i18n("Output Folder Does Not Exist"));
            return false;
        }
    }
    return true;
}
/**
 * Check if a file named "name" with extension "ext" already exists.
 * @param concept   the package
 * @param name      the name of the file
 * @param ext       the extension of the file
 * @return the valid filename or null
 */
QString SimpleCodeGenerator::overwritableName(UMLPackage* concept, const QString &name, const QString &ext)
{
    CodeGenerationPolicy *commonPolicy = UMLApp::app()->commonPolicy();
    QDir outputDir = commonPolicy->getOutputDirectory();
    QString filename = name + ext;
    if(!outputDir.exists(filename)) {
        m_fileMap.insert(concept,filename);
        return filename; //if not, "name" is OK and we have not much to to
    }

    int suffix;
    QPointer<OverwriteDialogue> overwriteDialogue =
        new OverwriteDialogue(filename, outputDir.absolutePath(),
                              m_applyToAllRemaining, kapp->activeWindow());
    switch(commonPolicy->getOverwritePolicy()) {  //if it exists, check the OverwritePolicy we should use
    case CodeGenerationPolicy::Ok:                //ok to overwrite file
        break;
    case CodeGenerationPolicy::Ask:               //ask if we can overwrite
        switch(overwriteDialogue->exec()) {
        case KDialog::Yes:  //overwrite file
            if ( overwriteDialogue->applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Ok);
            } else {
                m_applyToAllRemaining = false;
            }
            break;
        case KDialog::No: //generate similar name
            suffix = 1;
            while (1) {
                filename = name + "__" + QString::number(suffix) + ext;
                if (!outputDir.exists(filename))
                    break;
                suffix++;
            }
            if ( overwriteDialogue->applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Never);
            } else {
                m_applyToAllRemaining = false;
            }
            break;
        case KDialog::Cancel: //don't output anything
            if ( overwriteDialogue->applyToAllRemaining() ) {
                commonPolicy->setOverwritePolicy(CodeGenerationPolicy::Cancel);
            } else {
                m_applyToAllRemaining = false;
            }
            delete overwriteDialogue;
            return QString();
            break;
        }
        break;
    case CodeGenerationPolicy::Never: //generate similar name
        suffix = 1;
        while (1) {
            filename = name + "__" + QString::number(suffix) + ext;
            if (!outputDir.exists(filename))
                break;
            suffix++;
        }
        break;
    case CodeGenerationPolicy::Cancel: //don't output anything
        delete overwriteDialogue;
        return QString();
        break;
    }

    m_fileMap.insert(concept, filename);
    delete overwriteDialogue;
    return filename;
}