X-Git-Url: https://git.sven.stormbind.net/?a=blobdiff_plain;f=src%2Fexport-confluence.cpp;fp=src%2Fexport-confluence.cpp;h=e615a79b78d24483ab96ff0786e9fb12c686babd;hb=d483bd8e6523c23c6f1d8908a2e0611c2bc9ff4f;hp=0000000000000000000000000000000000000000;hpb=7dfa3fe589d1722d49681f42cdb0bf1e6efb5223;p=sven%2Fvym.git diff --git a/src/export-confluence.cpp b/src/export-confluence.cpp new file mode 100644 index 0000000..e615a79 --- /dev/null +++ b/src/export-confluence.cpp @@ -0,0 +1,435 @@ +#include "export-confluence.h" + +#include + +#include "attributeitem.h" +#include "branchobj.h" +#include "mainwindow.h" +#include "settings.h" +#include "warningdialog.h" +#include "xmlobj.h" + +extern QString flagsPath; +extern Main *mainWindow; +extern QString vymName; +extern QString vymVersion; +extern QString vymHome; +extern Settings settings; + +ExportConfluence::ExportConfluence() : ExportBase() { init(); } + +ExportConfluence::ExportConfluence(VymModel *m) : ExportBase(m) { init(); } + +void ExportConfluence::init() +{ + createNewPage = true; + exportName = "ConfluenceNewPage"; + + extension = ".html"; + + url = ""; + pageName = ""; + + agent = nullptr; +} + +void ExportConfluence::setCreateNewPage(bool b) {createNewPage = b; } + +void ExportConfluence::setURL(const QString &u) { url = u; } + +void ExportConfluence::setPageName(const QString &t) { pageName = t;} + +QString ExportConfluence::getBranchText(BranchItem *current) +{ + if (current) { + QRectF hr; + LinkableMapObj *lmo = current->getLMO(); + if (lmo) { + hr = ((BranchObj *)lmo)->getBBoxHeading(); + } + QString id = model->getSelectString(current); + QString heading = quoteMeta(current->getHeadingPlain()); + + // Numbering + QString number; + if (dia.useNumbering()) number = getSectionString(current) + " "; + + // Long headings are will have linebreaks by default + heading = heading.replace("\\n", " "); + + if (dia.useTextColor()) { + QColor c = current->getHeadingColor(); + QString cs = QString("rgb(%1,%2,%3);") + .arg(c.red()) + .arg(c.green()) + .arg(c.blue()); + heading = QString("%2") + .arg(cs) + .arg(number + heading); + } + + QString s; + + // Task flags + QString taskFlags; + /* + if (dia.useTaskFlags) + { + Task *task = current->getTask(); + if (task) + { + QString taskName = task->getIconString(); + taskFlags += QString("\"%2\"") .arg(taskName) .arg(QObject::tr("Flag: %1","Alt tag in + HTML export").arg(taskName)); + } + } + */ + + // User flags + QString userFlags; + /* + if (dia.useUserFlags) + { + foreach (QString flag, current->activeFlagNames()) // + better don't use activeFlagNames, won't work for userflags userFlags += + QString("\"%2\"") .arg(flag) + .arg(QObject::tr("Flag: %1","Alt tag in HTML + export").arg(flag)); + } + */ + + // URL + // + // + // + + // For URLs check, if there is already a Confluence user in an attribute + QString url; + AttributeItem *ai = current->getAttributeByKey("ConfluenceUser.userKey"); + if (ai) { + url = ai->getKey(); + s += QString(" ").arg(ai->getValue().toString()); + } else { + url = current->getURL(); + + if (!url.isEmpty()) { + if (url.contains(settings.value("/atlassian/confluence/url", + "---undefined---").toString()) && url.contains("&")) { + + // Fix ampersands in URL to Confluence itself + url = quoteMeta(url); + } + + s += QString("%2") + .arg(url) + .arg(taskFlags + heading + userFlags); + } else + s += taskFlags + heading + userFlags; + } + + // Include images + if (dia.includeImages()) + { + int imageCount = current->imageCount(); + ImageItem *image; + QString imagePath; + for (int i=0; i< imageCount; i++) + { + image = current->getImageNum(i); + imagePath = "image-" + image->getUuid().toString() + ".png"; + image->saveImage( dirPath + "/" + imagePath); + agent->addUploadAttachmentPath(imagePath); + s += "

"; + // Limit size + if (image->width() > 250) + s+= ""; + else + s += ""; + s += QString("

").arg(imagePath); + + } + } + + // Include note + if (!current->isNoteEmpty()) { + VymNote note = current->getNote(); + QString n; + if (note.isRichText()) { + n = note.getText(); + QRegExp re(""); + re.setMinimal(true); + re.setPattern(""); + n.replace(re, ""); + + re.setPattern(""); + n.replace(re, ""); + + re.setPattern(""); + n.replace(re, ""); + + re.setPattern(""); + n.replace(re, ""); + + re.setPattern(".*"); + n.replace(re, ""); + + re.setPattern(""); + n.replace(re,""); + + re.setPattern("&(?!\\w*;)"); + n.replace(re, "&"); + } + else { + n = current->getNoteASCII(0, 0); + n.replace("&", "&"); + n.replace("<", "<"); + n.replace(">", ">"); + if (current->getNote().getFontHint() == "fixed") + n = "
" + n + "
"; + else { + n = "

" + n + "

"; + n.replace("\n", "

"); + } + } + + s += "\n
\n" + n + + "\n
\n"; + } + return s; + } + return QString(); +} + +QString ExportConfluence::buildList(BranchItem *current) +{ + QString r; + + uint i = 0; + uint visChilds = 0; + + BranchItem *bi = current->getFirstBranch(); + + QString ind = "\n" + indent(current->depth() + 1, false); + + QString sectionBegin = ""; + QString sectionEnd = "" ; + QString itemBegin; + QString itemEnd; + + QString expandBegin; + QString expandEnd; + + switch (current->depth() + 1) { + case 0: + itemBegin = "

"; + itemEnd = "

"; + break; + case 1: + itemBegin = "

"; + itemEnd = "

"; + break; + case 2: + itemBegin = "

"; + itemEnd = "

"; + break; + default: + sectionBegin = + "
    depth() + 1) + ">"; + sectionEnd = "
"; + itemBegin = "
  • "; + itemEnd = "
  • "; + break; + } + + while (bi) { + if (bi && !bi->hasHiddenExportParent() && !bi->isHidden()) { + r += ind + sectionBegin; + if ( bi && bi->isScrolled()) + { + expandBegin = "\n" + ind; + expandBegin += QString("").arg(bi->getUuid().toString()) ; + expandBegin += ""; + expandEnd = "\n" + ind + ""; + expandEnd += ""; + } else + { + expandBegin = ""; + expandEnd = ""; + } + + if (!bi->hasHiddenExportParent() && !bi->isHidden() ) { + visChilds++; + r += ind; + r += itemBegin; + + // Check if first mapcenter is already usded for pageName + if ( !(bi == model->getRootItem()->getFirstBranch() && dia.mapCenterToPageName())) + r += getBranchText(bi); + + if (itemBegin.startsWith("getBranchNum(i); + } + + return r; +} + +QString ExportConfluence::createTOC() +{ + QString toc; + QString number; + toc += "\n"; + toc += "\n"; + toc += "\n"; + toc += "
    \n"; + toc += QObject::tr("Contents:", "Used in HTML export"); + toc += "\n"; + toc += "
    \n"; + BranchItem *cur = NULL; + BranchItem *prev = NULL; + model->nextBranch(cur, prev); + while (cur) { + if (!cur->hasHiddenExportParent() && !cur->hasScrolledParent()) { + if (dia.useNumbering()) + number = getSectionString(cur); + toc += + QString("
    ").arg(cur->depth()); + toc += QString(" %2 %3
    \n") + .arg(model->getSelectString(cur)) + .arg(number) + .arg(quoteMeta(cur->getHeadingPlain())); + toc += "
    "; + } + model->nextBranch(cur, prev); + } + toc += "
    \n"; + return toc; +} + +void ExportConfluence::doExport(bool useDialog) +{ + // Initialize tmp directory below tmp dir of map vym itself + setupTmpDir(); + + filePath = tmpDir.path() + "/export.html"; + + // Setup dialog and read settings + dia.setMapName(model->getMapName()); + dia.setFilePath(model->getFilePath()); + dia.setURL(url); + dia.setPageName(pageName); + BranchItem *bi = (BranchItem*)(model->findBySelectString("mc0")); + if (bi) + dia.setPageNameHint(bi->getHeadingPlain()); + + dia.readSettings(); + + if (useDialog) { + if (dia.exec() != QDialog::Accepted) + return; + model->setChanged(); + url = dia.getUrl(); + createNewPage = dia.getCreateNewPage(); + pageName = dia.getPageName(); + } + + // Open file for writing + QFile file(filePath); + if (!file.open(QIODevice::WriteOnly)) { + QMessageBox::critical( + 0, QObject::tr("Critical Export Error"), + QObject::tr("Trying to save HTML file:") + "\n\n" + + QObject::tr("Could not write %1").arg(filePath)); + return; + } + QTextStream ts(&file); + ts.setCodec("UTF-8"); + + // Hide stuff during export + model->setExportMode(true); + + // Create Confluence agent + agent = new ConfluenceAgent(); + if (createNewPage) + agent->setJobType(ConfluenceAgent::CreatePage); + else + agent->setJobType(ConfluenceAgent::UpdatePage); + agent->setPageURL(url); + agent->setNewPageName(pageName); + agent->setUploadPagePath(filePath); + agent->setModelID(model->getModelID()); + + // Include image of map + QString mapImageFilePath = tmpDir.path() + "/mapImage.png"; + if (dia.includeMapImage()) + { + offset = model->exportImage (mapImageFilePath, false, "PNG"); + QImage img(mapImageFilePath); + ts << "

    "; + ts << " \n"; + if (img.width() > 800) + ts << " "; + else + ts << " "; + ts << " "; + ts << " \n"; + ts << " "; + ts << "

    "; + + + agent->addUploadAttachmentPath(mapImageFilePath); + } + + // Include table of contents + // if (dia.useTOC) ts << createTOC(); + + // Main loop over all mapcenters + ts << buildList(model->getRootItem()) << "\n"; + + ts << "

    Page created with "; + ts << "" << vymName << " " << vymVersion<< ""; + ts << "

    "; + + file.close(); + + + agent->startJob(); + + QStringList args; + exportName = (createNewPage) ? "ConfluenceNewPage" : "ConfluenceUpdatePage"; + args << url; + if (!pageName.isEmpty()) + args << pageName; + + result = ExportBase::Ongoing; + + // Prepare human readable info in tooltip of LastExport: + displayedDestination = QString("Page name: \"%1\" Url: \"%2\"").arg(pageName).arg(url); + + completeExport(args); + + dia.saveSettings(); + model->setExportMode(false); + + // Note: ConfluenceAgent professionally destroys itself after completion +}