36 #include <QTextStream>
42 #include <QApplication>
58 QTimer::singleShot(100,
this, SLOT(startupLoadPatient()));
74 bool valid = this->isValidSessionFolder(dir);
75 bool exists = this->folderExists(dir);
79 this->loadSession(dir);
83 this->initializeNewSession(dir);
87 reportError(QString(
"Failed to initialize session in existing folder with no valid session: %1").arg(dir));
91 bool SessionStorageServiceImpl::isValidSessionFolder(QString dir)
const
93 QString filename = QDir(dir).absoluteFilePath(this->getXmlFileName());
94 return QFileInfo::exists(filename);
97 bool SessionStorageServiceImpl::folderExists(QString dir)
const
99 return QFileInfo(dir).exists() && QFileInfo(dir).isDir();
102 void SessionStorageServiceImpl::loadSession(QString dir)
104 if (this->isActivePatient(dir))
106 this->loadPatientSilent(dir);
107 this->reportActivePatient();
108 this->writeRecentPatientData();
111 void SessionStorageServiceImpl::initializeNewSession(QString dir)
113 this->clearPatientSilent();
114 dir = this->convertToValidFolderName(dir);
115 this->createPatientFolders(dir);
116 this->setActivePatient(dir);
118 this->reportActivePatient();
119 this->writeRecentPatientData();
122 QString SessionStorageServiceImpl::getXmlFileName()
const
124 return "custusdoc.xml";
136 this->generateSaveDoc(doc);
138 QDomElement element = doc.documentElement();
141 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
142 this->writeXmlFile(doc, filename);
144 report(
"Saved patient " + mActivePatientFolder);
148 void SessionStorageServiceImpl::writeXmlFile(QDomDocument doc, QString filename)
150 QFile file(filename);
151 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
153 QTextStream stream(&file);
154 stream << doc.toString(4);
159 reportError(
"Could not open " + file.fileName() +
" Error: " + file.errorString());
165 this->clearPatientSilent();
166 this->reportActivePatient();
167 this->writeRecentPatientData();
172 return !mActivePatientFolder.isEmpty() &&
178 return mActivePatientFolder;
181 void SessionStorageServiceImpl::reportActivePatient()
183 report(
"Set Active Patient: " + mActivePatientFolder);
186 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
188 if (activePatientFolder == mActivePatientFolder)
191 mActivePatientFolder = activePatientFolder;
196 void SessionStorageServiceImpl::onSessionChanged()
201 void SessionStorageServiceImpl::clearPatientSilent()
207 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const
209 return (patient == mActivePatientFolder);
212 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
214 if (this->isActivePatient(choosenDir))
216 this->clearPatientSilent();
217 if (choosenDir == QString::null)
220 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
221 QDomDocument doc = this->readXmlFile(filename);
223 mActivePatientFolder = choosenDir;
228 QDomElement element = doc.documentElement();
235 QDomDocument SessionStorageServiceImpl::readXmlFile(QString filename)
238 QFile file(filename);
239 if (!file.open(QIODevice::ReadOnly))
241 reportError(
"Could not open XML file :" + file.fileName() +
".");
242 return QDomDocument();
248 if (!retval.setContent(&file,
false, &emsg, &eline, &ecolumn))
250 reportError(
"Could not parse XML file :" + file.fileName() +
" because: " + emsg +
"");
251 return QDomDocument();
260 void SessionStorageServiceImpl::writeRecentPatientData()
266 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
268 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
270 QDomElement patientNode = doc.createElement(
"patient");
273 QDomElement versionName = doc.createElement(
"version_name");
274 versionName.appendChild(doc.createTextNode(this->getVersionName()));
275 patientNode.appendChild(versionName);
277 QDomElement activePatientNode = doc.createElement(
"active_patient");
278 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
279 patientNode.appendChild(activePatientNode);
280 doc.appendChild(patientNode);
283 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const
285 if (!dir.endsWith(
".cx3"))
290 void SessionStorageServiceImpl::createPatientFolders(QString dir)
293 QDir().mkpath(dir+
"/Images");
294 QDir().mkpath(dir+
"/Logs");
295 QDir().mkpath(dir+
"/US_Acq");
298 QString SessionStorageServiceImpl::getVersionName()
300 return QString(
"%1").arg(CustusX_VERSION_STRING);
303 void SessionStorageServiceImpl::clearCache()
313 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
315 int doLoad = QApplication::arguments().indexOf(
"--load");
318 if (doLoad + 1 >= QApplication::arguments().size())
321 QString folder = QApplication::arguments()[doLoad + 1];
328 void SessionStorageServiceImpl::startupLoadPatient()
330 QString folder = this->getCommandLineStartupPatient();
332 if (!folder.isEmpty())
334 report(QString(
"Startup Load [%1] from command line").arg(folder));
337 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
339 folder =
settings()->
value(
"startup/lastPatient").toString();
342 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
343 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
344 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
345 if (minsSinceLastSave > allowedMinsSinceLastSave)
348 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
349 .arg(
int(minsSinceLastSave/60))
350 .arg(
int(allowedMinsSinceLastSave/60)));
354 if (!folder.isEmpty())
355 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
358 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)
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
static QString getNoPatientFolder()
void fillDefault(QString name, T value)
SessionStorageServiceImpl(ctkPluginContext *context)
void isSaving(QDomElement &root)
xml storage is available
virtual QString getRootFolder() const