diff --git a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/DocumentLetterUtil.java b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/DocumentLetterUtil.java index fbe9fca316..205d1be1e0 100644 --- a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/DocumentLetterUtil.java +++ b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/DocumentLetterUtil.java @@ -13,12 +13,12 @@ import ch.elexis.core.model.IDocument; import ch.elexis.core.model.IDocumentLetter; import ch.elexis.core.model.IDocumentTemplate; +import ch.elexis.core.preferences.PreferencesUtil; +import ch.elexis.core.services.IVirtualFilesystemService; import ch.elexis.core.services.IVirtualFilesystemService.IVirtualFilesystemHandle; import ch.elexis.core.services.holder.ConfigServiceHolder; import ch.elexis.core.services.holder.CoreModelServiceHolder; import ch.elexis.core.services.holder.VirtualFilesystemServiceHolder; -import ch.elexis.core.utils.CoreUtil; -import ch.elexis.core.utils.CoreUtil.OS; import ch.rgw.tools.MimeTool; public class DocumentLetterUtil { @@ -34,117 +34,141 @@ public class DocumentLetterUtil { * @return */ public static @Nullable IVirtualFilesystemHandle getExternalHandleIfApplicable(IDocument document) { - if (document != null) { - try { - if (document instanceof IDocumentLetter) { - IDocumentLetter documentLetter = (IDocumentLetter) document; - if (ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE, false)) { - String path = getOperatingSystemSpecificExternalStoragePath(); - if (path != null) { - if (documentLetter.getPatient() != null) { - return getDocumentLetterFilePath(path, documentLetter); - } else if (documentLetter.isTemplate()) { - IDocumentTemplate documentTemplate = CoreModelServiceHolder.get() - .load(documentLetter.getId(), IDocumentTemplate.class).orElse(null); - if (documentTemplate != null) { - // make sure properties are correct if not yet saved to db - documentTemplate.setTitle(documentLetter.getTitle()); - documentTemplate.setMimeType(documentLetter.getMimeType()); - return getDocumentTemplateFilePath(path, documentTemplate); - } - } else { - logger.warn("No patient for [{}]", documentLetter.getId()); - } - - } else { - logger.warn("Brief external storage activate with null path"); - } - } - } else if (document instanceof IDocumentTemplate) { - if (ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE, false)) { - String path = getOperatingSystemSpecificExternalStoragePath(); - if (path != null) { - return getDocumentTemplateFilePath(path, (IDocumentTemplate) document); - } - } + if (document == null) { + return null; + } + + if (!ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE, false)) { + return null; + } + + String path = PreferencesUtil.getOsSpecificPreference(Preferences.P_TEXT_EXTERN_FILE_PATH, + ConfigServiceHolder.get()); + if (path == null) { + logger.error("External storage path is [null]"); + return null; + } + + IVirtualFilesystemHandle externalStoragePath; + try { + externalStoragePath = VirtualFilesystemServiceHolder.get().of(path); + } catch (IOException e) { + logger.error("Invalid external storage path [{}]", path, e.getMessage()); + return null; + } + + try { + // optimistic - we assume the external storage path exists and validate on the + // target of the resulting doc path only + if (document instanceof IDocumentLetter) { + IDocumentLetter documentLetter = (IDocumentLetter) document; + if (documentLetter.getPatient() != null) { + return getDocumentLetterFilePath(externalStoragePath, documentLetter); + } + if (documentLetter.isTemplate()) { + IDocumentTemplate documentTemplate = CoreModelServiceHolder.get() + .load(documentLetter.getId(), IDocumentTemplate.class).orElseThrow(); + // make sure properties are correct if not yet saved to db + documentTemplate.setTitle(documentLetter.getTitle()); + documentTemplate.setMimeType(documentLetter.getMimeType()); + return getDocumentTemplateFilePath(externalStoragePath, documentTemplate); } - } catch (IOException e) { - logger.warn("Error loading letter [{}]", document.getId(), e); + logger.warn("No patient set in IDocumentLetter and is no template [{}]", documentLetter.getId()); + } else if (document instanceof IDocumentTemplate) { + return getDocumentTemplateFilePath(externalStoragePath, (IDocumentTemplate) document); } + } catch (IOException e) { + logger.warn("Error loading letter [{}]", document.getId(), e); } return null; } - private static IVirtualFilesystemHandle getDocumentLetterFilePath(String path, IDocumentLetter documentLetter) - throws IOException { - IVirtualFilesystemHandle basePath = VirtualFilesystemServiceHolder.get().of(path); - if (basePath.exists() && basePath.canRead() && basePath.canWrite()) { - IVirtualFilesystemHandle patientSubDir = basePath.subDir(documentLetter.getPatient().getPatientNr()); - patientSubDir = patientSubDir.mkdir(); // assert existence - IVirtualFilesystemHandle filePath = patientSubDir - .subFile(documentLetter.getId() + "." + evaluateFileExtension(documentLetter.getMimeType())); - return filePath; - } else { - logger.warn("Base external storage path [{}] does not exist or is not read/writable", basePath); + private static IVirtualFilesystemHandle getDocumentLetterFilePath(IVirtualFilesystemHandle externalStoragePath, + IDocumentLetter documentLetter) throws IOException { + + IVirtualFilesystemHandle patientSubDir = externalStoragePath.subDir(documentLetter.getPatient().getPatientNr()); + if (!patientSubDir.exists()) { + if (!(externalStoragePath.canRead() && externalStoragePath.canWrite())) { + logger.error("External storage path [{}] does not exist or is not read/writable", externalStoragePath); + return null; + } + logger.info("mkdir [{}]", + IVirtualFilesystemService.hidePasswordInUrlString(patientSubDir.toURL().toString())); + patientSubDir.mkdir(); } - return null; + + IVirtualFilesystemHandle filePath = patientSubDir + .subFile(documentLetter.getId() + "." + evaluateFileExtension(documentLetter.getMimeType())); + return filePath; } - private static IVirtualFilesystemHandle getDocumentTemplateFilePath(String externalStoragePath, + private static IVirtualFilesystemHandle getDocumentTemplateFilePath(IVirtualFilesystemHandle externalStoragePath, IDocumentTemplate documentTemplate) throws IOException { - IVirtualFilesystemHandle basePath = VirtualFilesystemServiceHolder.get().of(externalStoragePath); - if (basePath.exists() && basePath.canRead() && basePath.canWrite()) { - IVirtualFilesystemHandle templatesSubDir = basePath.subDir("templates"); - templatesSubDir = templatesSubDir.mkdir(); // assert existence - IVirtualFilesystemHandle typedTemplatesSubDir = null; - if (BriefConstants.SYS_TEMPLATE.equals(documentTemplate.getTemplateTyp())) { - typedTemplatesSubDir = templatesSubDir.subDir("system"); - typedTemplatesSubDir = typedTemplatesSubDir.mkdir(); // assert existence - } else { - typedTemplatesSubDir = templatesSubDir.subDir("custom"); - typedTemplatesSubDir = typedTemplatesSubDir.mkdir(); // assert existence - } - IVirtualFilesystemHandle templatesFileSubDir = typedTemplatesSubDir; - if (documentTemplate.getMandator() != null) { - templatesFileSubDir = typedTemplatesSubDir.subDir(documentTemplate.getMandator().getLabel()); - templatesFileSubDir = templatesFileSubDir.mkdir(); // assert existence - } - IVirtualFilesystemHandle filePath = templatesFileSubDir.subFile(documentTemplate.getId() + "_" - + documentTemplate.getTitle() + "." + evaluateFileExtension(documentTemplate.getMimeType())); - return filePath; - } else { - logger.warn("Base external storage path [{}] does not exist or is not read/writable", basePath); + IVirtualFilesystemHandle templatesSubDir = externalStoragePath.subDir("templates"); + String _templatesSubDir = BriefConstants.SYS_TEMPLATE.equals(documentTemplate.getTemplateTyp()) ? "system" + : "custom"; + IVirtualFilesystemHandle typedTemplatesSubDir = templatesSubDir.subDir(_templatesSubDir); + IVirtualFilesystemHandle mandatorTypedTemplatesSubDir = null; + if (documentTemplate.getMandator() != null) { + mandatorTypedTemplatesSubDir = typedTemplatesSubDir.subDir(documentTemplate.getMandator().getLabel()); } - return null; - } - public static String getOperatingSystemSpecificExternalStoragePath() { - OS operatingSystem = CoreUtil.getOperatingSystemType(); - String setting; - switch (operatingSystem) { - case WINDOWS: - setting = Preferences.P_TEXT_EXTERN_FILE_PATH_WINDOWS; - break; - case MAC: - setting = Preferences.P_TEXT_EXTERN_FILE_PATH_MAC; - break; - case LINUX: - setting = Preferences.P_TEXT_EXTERN_FILE_PATH_LINUX; - break; - default: - setting = Preferences.P_TEXT_EXTERN_FILE_PATH; - break; - } - String path = ConfigServiceHolder.getGlobal(setting, null); - if (path == null) { - LoggerFactory.getLogger(DocumentLetterUtil.class) - .warn("No OS specific path set, reverting to generic setting"); - path = ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE_PATH, null); + IVirtualFilesystemHandle targetDirectory = mandatorTypedTemplatesSubDir != null ? mandatorTypedTemplatesSubDir + : typedTemplatesSubDir; + if (!targetDirectory.canRead()) { + if (!(externalStoragePath.canRead() && externalStoragePath.canWrite())) { + logger.error("External storage path [{}] does not exist or is not read/writable", externalStoragePath); + return null; + } + if (!templatesSubDir.exists()) { + logger.info("mkdir [{}]", + IVirtualFilesystemService.hidePasswordInUrlString(templatesSubDir.toURL().toString())); + templatesSubDir.mkdir(); + } + if (!typedTemplatesSubDir.exists()) { + logger.info("mkdir [{}]", + IVirtualFilesystemService.hidePasswordInUrlString(typedTemplatesSubDir.toURL().toString())); + typedTemplatesSubDir.mkdir(); + } + if (mandatorTypedTemplatesSubDir != null && !mandatorTypedTemplatesSubDir.exists()) { + logger.info("mkdir [{}]", IVirtualFilesystemService + .hidePasswordInUrlString(mandatorTypedTemplatesSubDir.toURL().toString())); + mandatorTypedTemplatesSubDir.mkdir(); + } } - return path; + + IVirtualFilesystemHandle targetFile = targetDirectory.subFile(documentTemplate.getId() + "_" + + documentTemplate.getTitle() + "." + evaluateFileExtension(documentTemplate.getMimeType())); + return targetFile; } +// public static String getOperatingSystemSpecificExternalStoragePath() { +// OS operatingSystem = CoreUtil.getOperatingSystemType(); +// String setting; +// switch (operatingSystem) { +// case WINDOWS: +// setting = Preferences.P_TEXT_EXTERN_FILE_PATH_WINDOWS; +// break; +// case MAC: +// setting = Preferences.P_TEXT_EXTERN_FILE_PATH_MAC; +// break; +// case LINUX: +// setting = Preferences.P_TEXT_EXTERN_FILE_PATH_LINUX; +// break; +// default: +// setting = Preferences.P_TEXT_EXTERN_FILE_PATH; +// break; +// } +// String path = ConfigServiceHolder.getGlobal(setting, null); +// if (path == null) { +// LoggerFactory.getLogger(DocumentLetterUtil.class) +// .warn("No OS specific path set, reverting to generic setting"); +// path = ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE_PATH, null); +// } +// return path; +// } + /** * Get the file extension part of the input String. * diff --git a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/internal/ModelUtil.java b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/internal/ModelUtil.java index 43b71b5110..271f90d3db 100644 --- a/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/internal/ModelUtil.java +++ b/bundles/ch.elexis.core.model/src/ch/elexis/core/model/util/internal/ModelUtil.java @@ -10,7 +10,6 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import ch.elexis.core.constants.Preferences; import ch.elexis.core.jpa.entities.EntityWithId; import ch.elexis.core.jpa.entities.Kontakt; import ch.elexis.core.jpa.entities.Userconfig; @@ -33,7 +32,6 @@ import ch.elexis.core.services.IQuery; import ch.elexis.core.services.IQuery.COMPARATOR; import ch.elexis.core.services.IStoreToStringContribution; -import ch.elexis.core.utils.CoreUtil; /** * Utility class with core model specific methods @@ -48,20 +46,6 @@ public class ModelUtil { private static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyyMMdd"); private static DateTimeFormatter defaultDateFormatter = DateTimeFormatter.ofPattern("dd.MM.yyyy"); - public static String getExternFilePath() { - return getAsExternFilePath(getConfig(Preferences.P_TEXT_EXTERN_FILE_PATH, null)); - } - - private static String getAsExternFilePath(String path) { - if (path != null && path.contains("[home]")) { - path = path.replace("[home]", CoreUtil.getWritableUserDir().getAbsolutePath()); - LoggerFactory.getLogger(ModelUtil.class) - .warn("Replaced [home] -> [" + CoreUtil.getWritableUserDir().getAbsolutePath() - + "] in extern file path result is [" + path + "]"); - } - return path; - } - /** * Test if there is a matching {@link Config} entry with a value that can be * interpreted as true. If no {@link Config} is present defaultValue is diff --git a/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/dialog/VirtualFilesystemUriEditorDialog.java b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/dialog/VirtualFilesystemUriEditorDialog.java index bf080c69b4..82fe5aa39e 100644 --- a/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/dialog/VirtualFilesystemUriEditorDialog.java +++ b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/dialog/VirtualFilesystemUriEditorDialog.java @@ -25,6 +25,7 @@ import org.eclipse.swt.events.FocusListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.Combo; import org.eclipse.swt.widgets.Composite; import org.eclipse.swt.widgets.Control; @@ -43,6 +44,15 @@ public class VirtualFilesystemUriEditorDialog extends TitleAreaDialog { private IVirtualFilesystemService virtualFilesystemService; private MyURI uri; + private Button browseButton; + + private Label lblScheme; + private Label lblHost; + private Label lblPort; + private Label lblPath; + private Label lblUser; + private Label lblPassword; + private Text txtHost; private Text txtPort; private Text txtUser; @@ -51,6 +61,9 @@ public class VirtualFilesystemUriEditorDialog extends TitleAreaDialog { private Text txtUri; private Combo comboScheme; private boolean passwortPhase = false; + + private String fixedScheme; + /** * Create the dialog. * @@ -81,43 +94,46 @@ protected Control createDialogArea(Composite parent) { container.setLayout(new GridLayout(2, false)); container.setLayoutData(new GridData(GridData.FILL_BOTH)); - Label lblScheme = new Label(container, SWT.NONE); + lblScheme = new Label(container, SWT.NONE); lblScheme.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblScheme.setText("Scheme"); comboScheme = new Combo(container, SWT.NONE); comboScheme.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); comboScheme.setItems("smb", "file"); + comboScheme.addListener(SWT.Selection, l -> { + updateButtonState(); + }); - Label lblHost = new Label(container, SWT.NONE); + lblHost = new Label(container, SWT.NONE); lblHost.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblHost.setText("Host"); txtHost = new Text(container, SWT.BORDER); txtHost.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - Label lblPort = new Label(container, SWT.NONE); + lblPort = new Label(container, SWT.NONE); lblPort.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblPort.setText("Port"); txtPort = new Text(container, SWT.BORDER); txtPort.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - Label lblPath = new Label(container, SWT.NONE); + lblPath = new Label(container, SWT.NONE); lblPath.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblPath.setText("Path"); txtPath = new Text(container, SWT.BORDER); txtPath.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - Label txtPath = new Label(container, SWT.NONE); - txtPath.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); - txtPath.setText("User"); + lblUser = new Label(container, SWT.NONE); + lblUser.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); + lblUser.setText("User"); txtUser = new Text(container, SWT.BORDER); txtUser.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); - Label lblPassword = new Label(container, SWT.NONE); + lblPassword = new Label(container, SWT.NONE); lblPassword.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1)); lblPassword.setText("Password"); @@ -153,9 +169,64 @@ public void focusLost(FocusEvent e) { txtUri = new Text(container, SWT.BORDER); txtUri.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1)); + updateUi(); + return area; } + private void updateUi() { + if (StringUtils.isNotBlank(fixedScheme)) { + hide(lblScheme); + hide(comboScheme); + if (fixedScheme.equals("file")) { + hide(lblHost); + hide(txtHost); + hide(lblPort); + hide(txtPort); + hide(lblUser); + hide(txtUser); + hide(lblPassword); + hide(txtPassword); + } + } else { + show(lblScheme); + show(comboScheme); + show(lblHost); + show(txtHost); + show(lblHost); + show(txtHost); + show(lblPort); + show(txtPort); + show(lblUser); + show(txtUser); + show(lblPassword); + show(txtPassword); + } + this.getShell().layout(true, true); + } + + private void hide(Control control) { + if (control.getLayoutData() instanceof GridData) { + ((GridData) control.getLayoutData()).exclude = true; + } + control.setVisible(false); + } + + private void show(Control control) { + if (control.getLayoutData() instanceof GridData) { + ((GridData) control.getLayoutData()).exclude = false; + } + control.setVisible(true); + } + + private void updateButtonState() { + if (fixedScheme == null) { + browseButton.setEnabled("file".equals(comboScheme.getText())); + } else { + browseButton.setEnabled("file".equals(fixedScheme)); + } + } + /** * Create contents of the button bar. * @@ -164,10 +235,12 @@ public void focusLost(FocusEvent e) { @Override protected void createButtonsForButtonBar(Composite parent) { createButton(parent, IDialogConstants.CLIENT_ID, "Test", false); - createButton(parent, 1025, JFaceResources.getString("openBrowse"), false); //$NON-NLS-1$ + browseButton = createButton(parent, 1025, JFaceResources.getString("openBrowse"), false); //$NON-NLS-1$ createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true); createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false); m_bindingContext = initDataBindings(); + + updateButtonState(); } @Override @@ -410,4 +483,14 @@ protected void firePropertyChange(String propertyName, Object oldValue, Object n } } + + /** + * The dialog shows only the provided scheme. Possible values are "smb", "file", + * "davs", "dav". + * + * @param scheme + */ + public void setFixedScheme(String scheme) { + this.fixedScheme = scheme; + } } diff --git a/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditor.java b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditor.java index 4f680c3464..8025aa54c1 100644 --- a/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditor.java +++ b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditor.java @@ -22,6 +22,8 @@ */ public class URIFieldEditor extends StringButtonFieldEditor { + private String scheme; + /** * * /** Creates a new directory field editor @@ -54,6 +56,12 @@ protected void doLoad() { } } + @Override + protected void doStore() { + // TODO Auto-generated method stub + super.doStore(); + } + @Override protected String changePressed() { IVirtualFilesystemService virtualFilesystemService = VirtualFilesystemServiceHolder.get(); @@ -68,11 +76,19 @@ protected String changePressed() { } VirtualFilesystemUriEditorDialog dialog = new VirtualFilesystemUriEditorDialog(getShell(), virtualFilesystemService, inputUri); - int open = dialog.open(); - if (IDialogConstants.OK_ID == open) { + dialog.setFixedScheme(scheme); + if (IDialogConstants.OK_ID == dialog.open()) { return dialog.getValue().toString(); } return null; } + /** + * Fix the possible URI scheme to the provided scheme. + * + * @param scheme + */ + public void setFixedScheme(String scheme) { + this.scheme = scheme; + } } diff --git a/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditorComposite.java b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditorComposite.java new file mode 100644 index 0000000000..31e2a6aa54 --- /dev/null +++ b/bundles/ch.elexis.core.ui.e4/src/ch/elexis/core/ui/e4/jface/preference/URIFieldEditorComposite.java @@ -0,0 +1,115 @@ +package ch.elexis.core.ui.e4.jface.preference; + +import org.apache.commons.lang3.StringUtils; +import org.eclipse.jface.preference.FieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.util.IPropertyChangeListener; +import org.eclipse.jface.util.PropertyChangeEvent; +import org.eclipse.jface.viewers.ArrayContentProvider; +import org.eclipse.jface.viewers.ComboViewer; +import org.eclipse.jface.viewers.LabelProvider; +import org.eclipse.jface.viewers.StructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.widgets.Combo; +import org.eclipse.swt.widgets.Composite; + +import ch.elexis.core.preferences.PreferencesUtil; +import ch.elexis.core.utils.CoreUtil; +import ch.elexis.core.utils.CoreUtil.OS; + +/** + * A {@link Composite} containing an operating system selector and a + * {@link URIFieldEditor} for file system URIs. The method + * {@link URIFieldEditorComposite#getPreferenceName(OS)} should be overwritten + * to select the correct preference. + * + */ +public class URIFieldEditorComposite extends Composite { + + private URIFieldEditor storePath; + + private String defaultPreference; + + private ComboViewer osCombo; + + private String scheme; + + public URIFieldEditorComposite(String defaultPreference, Composite parent, int style) { + super(parent, style); + setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 3, 1)); + + this.defaultPreference = defaultPreference; + + createContent(); + } + + /** + * Use this method to set the {@link IPreferenceStore} used by the + * {@link URIFieldEditor}. If a {@link FieldEditorPreferencePage} is available + * do not use this method, but set directly with + * {@link URIFieldEditorComposite#getFieldEditor()}.
+ * The field editor will store after each value change if this method is used. + * + * @param preferenceStore + */ + public void setPreferenceStore(IPreferenceStore preferenceStore) { + storePath.setPreferenceStore(preferenceStore); + storePath.load(); + + // add + storePath.setPropertyChangeListener(new IPropertyChangeListener() { + + @Override + public void propertyChange(PropertyChangeEvent event) { + storePath.store(); + } + }); + } + + private void createContent() { + Combo comboOs = new Combo(this, SWT.None); + osCombo = new ComboViewer(comboOs); + comboOs.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false)); + osCombo.setContentProvider(ArrayContentProvider.getInstance()); + osCombo.setLabelProvider(new LabelProvider() { + @Override + public String getText(Object element) { + return ((CoreUtil.OS) element).name(); + } + }); + osCombo.setInput(CoreUtil.OS.values()); + + storePath = new URIFieldEditor( + PreferencesUtil.getOsSpecificPreferenceName(CoreUtil.getOperatingSystemType(), defaultPreference), + StringUtils.EMPTY, this); + storePath.setEmptyStringAllowed(true); + + osCombo.addSelectionChangedListener(event -> { + CoreUtil.OS selection = (OS) event.getStructuredSelection().getFirstElement(); + String preferenceName = PreferencesUtil.getOsSpecificPreferenceName(selection, defaultPreference); + storePath.store(); + storePath.setPreferenceName(preferenceName); + storePath.load(); + }); + + osCombo.setSelection(new StructuredSelection(CoreUtil.getOperatingSystemType())); + } + + public FieldEditor getFieldEditor() { + return storePath; + } + + /** + * Fix the possible URI scheme to the provided scheme. + * + * @param scheme + */ + public void setFixedScheme(String scheme) { + this.scheme = scheme; + if (storePath != null) { + storePath.setFixedScheme("file"); + } + } +} diff --git a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/commands/StartEditLocalDocumentHandler.java b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/commands/StartEditLocalDocumentHandler.java index bf77a9de82..023191fa84 100644 --- a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/commands/StartEditLocalDocumentHandler.java +++ b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/commands/StartEditLocalDocumentHandler.java @@ -27,6 +27,7 @@ import ch.elexis.core.model.IDocumentLetter; import ch.elexis.core.model.Identifiable; import ch.elexis.core.model.util.DocumentLetterUtil; +import ch.elexis.core.preferences.PreferencesUtil; import ch.elexis.core.services.IConflictHandler; import ch.elexis.core.services.IElexisServerService.ConnectionStatus; import ch.elexis.core.services.ILocalDocumentService; @@ -132,7 +133,8 @@ public void lockAcquired() { public static void convertDocx2Pdf(Optional file) { for (String format : List.of("pdf", "txt")) { //$NON-NLS-1$ //$NON-NLS-2$ String filePath = file.get().getAbsolutePath(); - String storage = DocumentLetterUtil.getOperatingSystemSpecificExternalStoragePath(); + String storage = PreferencesUtil.getOsSpecificPreference(Preferences.P_TEXT_EXTERN_FILE_PATH, + ConfigServiceHolder.get()); String fullCmd = String.format("libreoffice --headless --convert-to %s --outdir %s %s", format, storage, //$NON-NLS-1$ filePath); logger.info("Convert external file using"); //$NON-NLS-1$ diff --git a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/preferences/Texterstellung.java b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/preferences/Texterstellung.java index b211979a65..a9992d8495 100644 --- a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/preferences/Texterstellung.java +++ b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/preferences/Texterstellung.java @@ -50,6 +50,7 @@ import ch.elexis.core.data.activator.CoreHub; import ch.elexis.core.data.util.Extensions; import ch.elexis.core.l10n.Messages; +import ch.elexis.core.preferences.PreferencesUtil; import ch.elexis.core.services.ILocalDocumentService; import ch.elexis.core.services.holder.ConfigServiceHolder; import ch.elexis.core.ui.constants.ExtensionPointConstantsUi; @@ -164,23 +165,8 @@ public String getText(Object element) { cvOs.addSelectionChangedListener(event -> { CoreUtil.OS selection = (OS) event.getStructuredSelection().getFirstElement(); - String preferenceName; - switch (selection) { - case MAC: - preferenceName = Preferences.P_TEXT_EXTERN_FILE_PATH_MAC; - break; - case WINDOWS: - preferenceName = Preferences.P_TEXT_EXTERN_FILE_PATH_WINDOWS; - break; - case LINUX: - preferenceName = Preferences.P_TEXT_EXTERN_FILE_PATH_LINUX; - break; - default: - preferenceName = Preferences.P_TEXT_EXTERN_FILE_PATH; - break; - } storePath.store(); - storePath.setPreferenceName(preferenceName); + storePath.setPreferenceName(PreferencesUtil.getOsSpecificPreferenceName(selection, Preferences.P_TEXT_EXTERN_FILE_PATH)); storePath.load(); }); diff --git a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/property/PropertyTester.java b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/property/PropertyTester.java index 0860633a1e..9688dbf396 100644 --- a/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/property/PropertyTester.java +++ b/bundles/ch.elexis.core.ui/src/ch/elexis/core/ui/property/PropertyTester.java @@ -7,7 +7,7 @@ import ch.elexis.core.data.util.BriefExternUtil; import ch.elexis.core.model.IEncounter; import ch.elexis.core.model.InvoiceState; -import ch.elexis.core.model.util.DocumentLetterUtil; +import ch.elexis.core.preferences.PreferencesUtil; import ch.elexis.core.services.holder.ConfigServiceHolder; import ch.elexis.core.services.holder.ContextServiceHolder; @@ -22,7 +22,8 @@ public boolean test(Object receiver, String property, Object[] args, Object expe if ("documenteditlocal".equals(property)) { //$NON-NLS-1$ // no local copy / local edit if brief extern if (ConfigServiceHolder.getGlobal(Preferences.P_TEXT_EXTERN_FILE, false) && BriefExternUtil - .isValidExternPath(DocumentLetterUtil.getOperatingSystemSpecificExternalStoragePath(), false)) { + .isValidExternPath(PreferencesUtil.getOsSpecificPreference(Preferences.P_TEXT_EXTERN_FILE_PATH, + ConfigServiceHolder.get()), false)) { return false; } return CoreHub.localCfg.get(Preferences.P_TEXT_EDIT_LOCAL, false); diff --git a/bundles/ch.elexis.core/src/ch/elexis/core/constants/Preferences.java b/bundles/ch.elexis.core/src/ch/elexis/core/constants/Preferences.java index 51d7cfbb54..ed4c8f0ed3 100644 --- a/bundles/ch.elexis.core/src/ch/elexis/core/constants/Preferences.java +++ b/bundles/ch.elexis.core/src/ch/elexis/core/constants/Preferences.java @@ -101,9 +101,6 @@ public class Preferences { public static final String P_TEXT_EDIT_LOCAL = "briefe/Textmodul_Edit_Local"; //$NON-NLS-1$ public static final String P_TEXT_EXTERN_FILE = "briefe/Textmodul_Extern_File"; //$NON-NLS-1$ public static final String P_TEXT_EXTERN_FILE_PATH = "briefe/Textmodul_Extern_File_Path"; //$NON-NLS-1$ - public static final String P_TEXT_EXTERN_FILE_PATH_WINDOWS = "briefe/Textmodul_Extern_File_Path_WINDOWS"; //$NON-NLS-1$ - public static final String P_TEXT_EXTERN_FILE_PATH_MAC = "briefe/Textmodul_Extern_File_Path_MAC"; //$NON-NLS-1$ - public static final String P_TEXT_EXTERN_FILE_PATH_LINUX = "briefe/Textmodul_Extern_File_Path_LINUX"; //$NON-NLS-1$ // Gruppen und Rechte public static final String ACC_GROUPS = "groupNames"; //$NON-NLS-1$ diff --git a/bundles/ch.elexis.core/src/ch/elexis/core/preferences/PreferencesUtil.java b/bundles/ch.elexis.core/src/ch/elexis/core/preferences/PreferencesUtil.java new file mode 100644 index 0000000000..f0366331fb --- /dev/null +++ b/bundles/ch.elexis.core/src/ch/elexis/core/preferences/PreferencesUtil.java @@ -0,0 +1,37 @@ +package ch.elexis.core.preferences; + +import org.apache.commons.lang3.StringUtils; +import org.slf4j.LoggerFactory; + +import ch.elexis.core.services.IConfigService; +import ch.elexis.core.utils.CoreUtil; +import ch.elexis.core.utils.CoreUtil.OS; + +public class PreferencesUtil { + + public static String getOsSpecificPreferenceName(CoreUtil.OS system, String defaultPreference) { + switch (system) { + case WINDOWS: + return defaultPreference + "_WINDOWS"; + case MAC: + return defaultPreference + "_MAC"; + case LINUX: + return defaultPreference + "_LINUX"; + default: + return defaultPreference; + } + } + + public static String getOsSpecificPreference(String defaultPreference, + IConfigService configService) { + OS operatingSystem = CoreUtil.getOperatingSystemType(); + String osSpecificPreference = getOsSpecificPreferenceName(operatingSystem, defaultPreference); + String value = configService.get(osSpecificPreference, null); + if (StringUtils.isBlank(value)) { + LoggerFactory.getLogger(PreferencesUtil.class) + .warn("No OS specific path set, reverting to generic setting"); + value = configService.get(defaultPreference, null); + } + return value; + } +}