MCPcopy Create free account
hub / github.com/KDE/labplot / loadNotebook

Method loadNotebook

src/backend/core/Project.cpp:723–831  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

721
722#ifndef SDK
723bool Project::loadNotebook(const QString& filename) {
724 bool rc = false;
725#ifdef HAVE_CANTOR_LIBS
726 QString errorMessage;
727 QFile file(filename);
728 if (QFileInfo(filename).completeSuffix() == QLatin1String("cws")) {
729 KZip archive(&file);
730 rc = archive.open(QIODevice::ReadOnly);
731 if (rc) {
732 const auto* contentEntry = archive.directory()->entry(QLatin1String("content.xml"));
733 if (contentEntry && contentEntry->isFile()) {
734 const auto* contentFile = static_cast<const KArchiveFile*>(contentEntry);
735 QByteArray data = contentFile->data();
736 archive.close();
737
738 // determine the name of the backend
739 QDomDocument doc;
740 doc.setContent(data);
741 QString backendName = doc.documentElement().attribute(QLatin1String("backend"));
742
743 if (!backendName.isEmpty()) {
744 // create new notebook and load the data
745 auto* notebook = new Notebook(backendName);
746 notebook->setName(QFileInfo(filename).fileName());
747 notebook->setComment(filename);
748
749 rc = file.open(QIODevice::ReadOnly);
750 if (rc) {
751 QByteArray content = file.readAll();
752 rc = notebook->init(&content);
753 if (rc)
754 addChild(notebook);
755 else
756 delete notebook;
757 } else
758 errorMessage = i18n("Failed to open the file '%1'.", filename);
759 } else
760 rc = false;
761 } else
762 rc = false;
763 } else
764 errorMessage = i18n("Failed to open the file '%1'.", filename);
765 } else if (QFileInfo(filename).completeSuffix() == QLatin1String("ipynb")) {
766 rc = file.open(QIODevice::ReadOnly);
767 if (rc) {
768 QByteArray content = file.readAll();
769 QJsonParseError error;
770 // TODO: use QJsonDocument& doc = QJsonDocument::fromJson(content, &error); if minimum Qt version is at least 5.10
771 const QJsonDocument& jsonDoc = QJsonDocument::fromJson(content, &error);
772 const QJsonObject& doc = jsonDoc.object();
773 if (error.error == QJsonParseError::NoError) {
774 // determine the backend name
775 QString backendName;
776 // TODO: use doc["metadata"]["kernelspec"], etc. if minimum Qt version is at least 5.10
777 if ((doc[QLatin1String("metadata")] != QJsonValue::Undefined && doc[QLatin1String("metadata")].isObject())
778 && (doc[QLatin1String("metadata")].toObject()[QLatin1String("kernelspec")] != QJsonValue::Undefined
779 && doc[QLatin1String("metadata")].toObject()[QLatin1String("kernelspec")].isObject())) {
780 QString kernel;

Callers 1

openProjectMethod · 0.80

Calls 9

setCommentMethod · 0.80
openMethod · 0.45
dataMethod · 0.45
closeMethod · 0.45
isEmptyMethod · 0.45
setNameMethod · 0.45
fileNameMethod · 0.45
initMethod · 0.45
toStringMethod · 0.45

Tested by

no test coverage detected