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";
139 this->generateSaveDoc(doc);
141 QDomElement element = doc.documentElement();
144 QString filename = QDir(mActivePatientFolder).absoluteFilePath(this->getXmlFileName());
145 this->writeXmlFile(doc, filename);
147 report(
"Saved patient " + mActivePatientFolder);
151 void SessionStorageServiceImpl::writeXmlFile(QDomDocument doc, QString filename)
153 QFile file(filename);
154 if (file.open(QIODevice::WriteOnly | QIODevice::Truncate))
156 QTextStream stream(&file);
157 stream << doc.toString(4);
162 reportError(
"Could not open " + file.fileName() +
" Error: " + file.errorString());
168 this->clearPatientSilent();
169 this->reportActivePatient();
170 this->writeRecentPatientData();
175 return !mActivePatientFolder.isEmpty() &&
176 (mActivePatientFolder != this->getNoPatientFolder());
181 return mActivePatientFolder;
184 void SessionStorageServiceImpl::reportActivePatient()
186 report(
"Set Active Patient: " + mActivePatientFolder);
189 void SessionStorageServiceImpl::setActivePatient(
const QString& activePatientFolder)
191 if (activePatientFolder == mActivePatientFolder)
194 mActivePatientFolder = activePatientFolder;
199 void SessionStorageServiceImpl::onSessionChanged()
204 void SessionStorageServiceImpl::clearPatientSilent()
206 this->setActivePatient(this->getNoPatientFolder());
210 bool SessionStorageServiceImpl::isActivePatient(QString patient)
const
212 return (patient == mActivePatientFolder);
215 void SessionStorageServiceImpl::loadPatientSilent(QString choosenDir)
217 if (this->isActivePatient(choosenDir))
219 this->clearPatientSilent();
220 if (choosenDir == QString::null)
223 QString filename = QDir(choosenDir).absoluteFilePath(this->getXmlFileName());
224 QDomDocument doc = this->readXmlFile(filename);
226 mActivePatientFolder = choosenDir;
231 QDomElement element = doc.documentElement();
239 QDomDocument SessionStorageServiceImpl::readXmlFile(QString filename)
242 QFile file(filename);
243 if (!file.open(QIODevice::ReadOnly))
245 reportError(
"Could not open XML file :" + file.fileName() +
".");
246 return QDomDocument();
252 if (!retval.setContent(&file,
false, &emsg, &eline, &ecolumn))
254 reportError(
"Could not parse XML file :" + file.fileName() +
" because: " + emsg +
"");
255 return QDomDocument();
264 void SessionStorageServiceImpl::writeRecentPatientData()
270 void SessionStorageServiceImpl::generateSaveDoc(QDomDocument& doc)
272 doc.appendChild(doc.createProcessingInstruction(
"xml version =",
"'1.0'"));
274 QDomElement patientNode = doc.createElement(
"patient");
277 QDomElement versionName = doc.createElement(
"version_name");
278 versionName.appendChild(doc.createTextNode(this->getVersionName()));
279 patientNode.appendChild(versionName);
281 QDomElement activePatientNode = doc.createElement(
"active_patient");
282 activePatientNode.appendChild(doc.createTextNode(mActivePatientFolder.toStdString().c_str()));
283 patientNode.appendChild(activePatientNode);
284 doc.appendChild(patientNode);
287 QString SessionStorageServiceImpl::convertToValidFolderName(QString dir)
const
289 if (!dir.endsWith(
".cx3"))
294 void SessionStorageServiceImpl::createPatientFolders(QString dir)
297 QDir().mkpath(dir+
"/Images");
298 QDir().mkpath(dir+
"/Logs");
299 QDir().mkpath(dir+
"/US_Acq");
302 QString SessionStorageServiceImpl::getVersionName()
304 return QString(
"%1").arg(CustusX_VERSION_STRING);
307 void SessionStorageServiceImpl::clearCache()
317 QString SessionStorageServiceImpl::getCommandLineStartupPatient()
319 int doLoad = QApplication::arguments().indexOf(
"--load");
322 if (doLoad + 1 >= QApplication::arguments().size())
325 QString folder = QApplication::arguments()[doLoad + 1];
332 void SessionStorageServiceImpl::startupLoadPatient()
334 QString folder = this->getCommandLineStartupPatient();
336 if (!folder.isEmpty())
338 report(QString(
"Startup Load [%1] from command line").arg(folder));
341 if (folder.isEmpty() &&
settings()->
value(
"Automation/autoLoadRecentPatient").toBool())
343 folder =
settings()->
value(
"startup/lastPatient").toString();
344 if(this->isValidSessionFolder(folder))
347 double minsSinceLastSave = lastSaveTime.secsTo(QDateTime::currentDateTime())/60;
348 double autoLoadRecentPatientWithinHours =
settings()->
value(
"Automation/autoLoadRecentPatientWithinHours").toDouble();
349 int allowedMinsSinceLastSave = autoLoadRecentPatientWithinHours*60;
350 if (minsSinceLastSave > allowedMinsSinceLastSave)
353 QString(
"Startup Load: Ignored recent patient because %1 hours since last save, limit is %2")
354 .arg(
int(minsSinceLastSave/60))
355 .arg(
int(allowedMinsSinceLastSave/60)));
358 if (!folder.isEmpty())
359 report(QString(
"Startup Load [%1] as recent patient").arg(folder));
363 report(
"Startup Load: Ignored recent patient because it is not valid anymore");
369 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