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";
137 this->generateSaveDoc(doc);
139 QDomElement element = doc.documentElement();
142 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
143 this->writeXmlFile(doc, filename);
145 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() &&
173 (mActivePatientFolder != this->getNoPatientFolder());
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()
203 this->setActivePatient(this->getNoPatientFolder());
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();
236 QDomDocument SessionStorageServiceImpl::readXmlFile(QString filename)
239 QFile file(filename);
240 if (!file.open(QIODevice::ReadOnly))
242 reportError(
"Could not open XML file :" + file.fileName() +
".");
243 return QDomDocument();
249 if (!retval.setContent(&file,
false, &emsg, &eline, &ecolumn))
251 reportError(
"Could not parse XML file :" + file.fileName() +
" because: " + emsg +
"");
252 return QDomDocument();
261 void SessionStorageServiceImpl::writeRecentPatientData()
267 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
269 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
271 QDomElement patientNode = doc.createElement(
"patient");
274 QDomElement versionName = doc.createElement(
"version_name");
275 versionName.appendChild(doc.createTextNode(this->getVersionName()));
276 patientNode.appendChild(versionName);
278 QDomElement activePatientNode = doc.createElement(
"active_patient");
279 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
280 patientNode.appendChild(activePatientNode);
281 doc.appendChild(patientNode);
284 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const
286 if (!dir.endsWith(
".cx3"))
291 void SessionStorageServiceImpl::createPatientFolders(QString dir)
294 QDir().mkpath(dir+
"/Images");
295 QDir().mkpath(dir+
"/Logs");
296 QDir().mkpath(dir+
"/US_Acq");
299 QString SessionStorageServiceImpl::getVersionName()
301 return QString(
"%1").arg(CustusX_VERSION_STRING);
304 void SessionStorageServiceImpl::clearCache()
314 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
316 int doLoad = QApplication::arguments().indexOf(
"--load");
319 if (doLoad + 1 >= QApplication::arguments().size())
322 QString folder = QApplication::arguments()[doLoad + 1];
329 void SessionStorageServiceImpl::startupLoadPatient()
331 QString folder = this->getCommandLineStartupPatient();
333 if (!folder.isEmpty())
335 report(QString(
"Startup Load [%1] from command line").arg(folder));
338 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
340 folder =
settings()->
value(
"startup/lastPatient").toString();
341 if(this->isValidSessionFolder(folder))
344 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
345 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
346 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
347 if (minsSinceLastSave > allowedMinsSinceLastSave)
350 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
351 .arg(
int(minsSinceLastSave/60))
352 .arg(
int(allowedMinsSinceLastSave/60)));
355 if (!folder.isEmpty())
356 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
360 report(
"Startup Load: Ignored recent patient because it is not valid anymore");
366 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