36 #include <QTextStream>
42 #include <QApplication>
53 mActivePatientFolder = this->getNoPatientFolder();
56 QTimer::singleShot(100,
this, SLOT(startupLoadPatient()));
71 bool valid = this->isValidSessionFolder(dir);
72 bool exists = this->folderExists(dir);
76 this->loadSession(dir);
80 this->initializeNewSession(dir);
84 reportError(QString(
"Failed to initialize session in existing folder with no valid session: %1").arg(dir));
88 QString SessionStorageServiceImpl::getNoPatientFolder()
const
94 bool SessionStorageServiceImpl::isValidSessionFolder(QString dir)
const
96 QString filename = QDir(dir).absoluteFilePath(this->getXmlFileName());
97 return QFileInfo::exists(filename);
100 bool SessionStorageServiceImpl::folderExists(QString dir)
const
102 return QFileInfo(dir).exists() && QFileInfo(dir).isDir();
105 void SessionStorageServiceImpl::loadSession(QString dir)
107 if (this->isActivePatient(dir))
109 this->loadPatientSilent(dir);
110 this->reportActivePatient();
111 this->writeRecentPatientData();
114 void SessionStorageServiceImpl::initializeNewSession(QString dir)
116 this->clearPatientSilent();
117 dir = this->convertToValidFolderName(dir);
118 this->createPatientFolders(dir);
119 this->setActivePatient(dir);
121 this->reportActivePatient();
122 this->writeRecentPatientData();
125 QString SessionStorageServiceImpl::getXmlFileName()
const
127 return "custusdoc.xml";
141 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
143 qSetGlobalQHashSeed(42);
145 mDoc = QDomDocument();
149 #if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
151 qSetGlobalQHashSeed(-1);
155 QDomDocument&
doc() {
return mDoc; }
167 this->generateSaveDoc(doc.
doc());
169 QDomElement element = doc.
doc().documentElement();
172 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
173 this->writeXmlFile(doc.
doc(), filename);
174 report(
"Saved patient " + mActivePatientFolder);
177 void SessionStorageServiceImpl::writeXmlFile(QDomDocument doc, QString filename)
179 QFile file(filename);
180 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
182 QTextStream stream(&file);
183 stream << doc.toString(4);
188 reportError(
"Could not open " + file.fileName() +
" Error: " + file.errorString());
194 this->clearPatientSilent();
195 this->reportActivePatient();
196 this->writeRecentPatientData();
201 return !mActivePatientFolder.isEmpty() &&
202 (mActivePatientFolder != this->getNoPatientFolder());
207 return mActivePatientFolder;
210 void SessionStorageServiceImpl::reportActivePatient()
212 report(
"Set Active Patient: " + mActivePatientFolder);
215 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
217 if (activePatientFolder == mActivePatientFolder)
220 mActivePatientFolder = activePatientFolder;
225 void SessionStorageServiceImpl::onSessionChanged()
230 void SessionStorageServiceImpl::clearPatientSilent()
232 this->setActivePatient(this->getNoPatientFolder());
236 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const
238 return (patient == mActivePatientFolder);
241 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
243 if (this->isActivePatient(choosenDir))
245 this->clearPatientSilent();
246 if (choosenDir == QString::null)
249 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
250 QDomDocument doc = this->readXmlFile(filename);
252 mActivePatientFolder = choosenDir;
257 QDomElement element = doc.documentElement();
265 QDomDocument SessionStorageServiceImpl::readXmlFile(QString filename)
268 QFile file(filename);
269 if (!file.open(QIODevice::ReadOnly))
271 reportError(
"Could not open XML file :" + file.fileName() +
".");
272 return QDomDocument();
278 if (!retval.setContent(&file,
false, &emsg, &eline, &ecolumn))
280 reportError(
"Could not parse XML file :" + file.fileName() +
" because: " + emsg +
"");
281 return QDomDocument();
290 void SessionStorageServiceImpl::writeRecentPatientData()
296 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
298 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
300 QDomElement patientNode = doc.createElement(
"patient");
303 QDomElement versionName = doc.createElement(
"version_name");
304 versionName.appendChild(doc.createTextNode(this->getVersionName()));
305 patientNode.appendChild(versionName);
307 QDomElement activePatientNode = doc.createElement(
"active_patient");
308 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
309 patientNode.appendChild(activePatientNode);
310 doc.appendChild(patientNode);
313 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const
315 if (!dir.endsWith(
".cx3"))
320 void SessionStorageServiceImpl::createPatientFolders(QString dir)
323 QDir().mkpath(dir+
"/Images");
324 QDir().mkpath(dir+
"/Logs");
325 QDir().mkpath(dir+
"/US_Acq");
328 QString SessionStorageServiceImpl::getVersionName()
330 return QString(
"%1").arg(CustusX_VERSION_STRING);
333 void SessionStorageServiceImpl::clearCache()
343 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
345 int doLoad = QApplication::arguments().indexOf(
"--load");
348 if (doLoad + 1 >= QApplication::arguments().size())
351 QString folder = QApplication::arguments()[doLoad + 1];
358 void SessionStorageServiceImpl::startupLoadPatient()
360 QString folder = this->getCommandLineStartupPatient();
362 if (!folder.isEmpty())
364 report(QString(
"Startup Load [%1] from command line").arg(folder));
367 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
369 folder =
settings()->
value(
"startup/lastPatient").toString();
370 if(this->isValidSessionFolder(folder))
373 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
374 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
375 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
376 if (minsSinceLastSave > allowedMinsSinceLastSave)
379 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
380 .arg(
int(minsSinceLastSave/60))
381 .arg(
int(allowedMinsSinceLastSave/60)));
384 if (!folder.isEmpty())
385 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
389 report(
"Startup Load: Ignored recent patient because it is not valid anymore");
395 if (folder.isEmpty())
virtual bool isNull() const
void reportError(QString msg)
void isLoading(QDomElement &root)
emitted while loading a session. Xml storage is available, getRootFolder() is set to loaded value...
bool removeNonemptyDirRecursively(const QString &dirName)
void isLoadingSecond(QDomElement &root)
Emitted after the isLoading signal, to allow for structures that must be loaded after core structures...
virtual void save()
Save all application data to XML file.
virtual void load(QString dir)
load session from dir, or create new session in this location if none exist
virtual bool isValid() const
virtual ~SessionStorageServiceImpl()
QVariant value(const QString &key, const QVariant &defaultValue=QVariant()) const
QString timestampSecondsFormat()
void sessionChanged()
emitted after change to a new session (new or loaded or cleared)
void setValue(const QString &key, const QVariant &value)
static QString getCachePath()
return path to a folder that is used during execution, will be cleared at start and stop...
QString getSubFolder(QString relative)
return and create a folder/path relative to root. Created if necessary.
Settings * settings()
Shortcut for accessing the settings instance.
void cleared()
emitted when session is cleared, before isLoading is called
SessionStorageServiceImpl(ctkPluginContext *context)
void isSaving(QDomElement &root)
xml storage is available
virtual QString getRootFolder() const