Skip to content

TagSpaces is an offline, open source, document manager with tagging support

Create mui-dialog for importing macos tags with option for disabling … (#2346)

Stanimir6ad87c87af73
diff --git a/src/main/util.ts b/src/main/util.ts
index 5e46f9cfd..a9497bddb 100644
--- a/src/main/util.ts
+++ b/src/main/util.ts
@@ -238,7 +238,12 @@ export async function readMacOSTags(filename) {
return new Promise((resolve, reject) => {
const args = ['-raw', '-name', 'kMDItemUserTags', filename];
execFile('mdls', args, (error, stdout, stderr) => {
- if (error) return reject(error);
+ if (error) {
+ if (error.code === 1 && !stderr) {
+ return resolve([]);
+ }
+ return reject(error);
+ }
if (stderr) return reject(new Error(stderr));
const text = stdout.trim();
diff --git a/src/renderer/components/dialogs/ImportMacTagDialog.tsx b/src/renderer/components/dialogs/ImportMacTagDialog.tsx
new file mode 100644
index 000000000..f934059a2
--- /dev/null
+++ b/src/renderer/components/dialogs/ImportMacTagDialog.tsx
@@ -0,0 +1,170 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-present TagSpaces GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+import React, { ChangeEvent, useReducer, useRef } from 'react';
+import DraggablePaper from '-/components/DraggablePaper';
+import DialogCloseButton from '-/components/dialogs/DialogCloseButton';
+import { Switch } from '@mui/material';
+import Dialog from '@mui/material/Dialog';
+import DialogContent from '@mui/material/DialogContent';
+import DialogTitle from '@mui/material/DialogTitle';
+import { useTranslation } from 'react-i18next';
+import TsDialogActions from '-/components/dialogs/components/TsDialogActions';
+import TsButton from '-/components/TsButton';
+import { Pro } from '-/pro';
+import { readMacOSTags } from '-/services/utils-io';
+import { useProgressDialogContext } from '-/components/dialogs/hooks/useProgressDialogContext';
+import { useTaggingActionsContext } from '-/hooks/useTaggingActionsContext';
+import { useCurrentLocationContext } from '-/hooks/useCurrentLocationContext';
+import { useNotificationContext } from '-/hooks/useNotificationContext';
+import { BetaLabel, ProLabel } from '-/components/HelperComponents';
+import FormControlLabel from '@mui/material/FormControlLabel';
+
+interface Props {
+ open: boolean;
+ onClose: () => void;
+ directoryPath: string;
+}
+
+function ImportMacTagDialog(props: Props) {
+ const { t } = useTranslation();
+ const { open, onClose, directoryPath } = props;
+
+ const { openProgressDialog, closeProgressDialog } =
+ useProgressDialogContext();
+ const { showNotification } = useNotificationContext();
+ const { currentLocation } = useCurrentLocationContext();
+ const { addTags } = useTaggingActionsContext();
+ const recursive = useRef<boolean>(false);
+ const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
+
+ const toggleRecursive = (event: React.ChangeEvent<HTMLInputElement>) => {
+ recursive.current = event.target.checked;
+ forceUpdate();
+ };
+
+ function importMacTags() {
+ if (Pro && Pro.MacTagsImport && Pro.MacTagsImport.importTags) {
+ onClose();
+ /*if (
+ !confirm(`Experimental feature\n
+Depending on how many tags you have in your current directory, the tag extraction process may take a long time in which the application's user interface may appear as blocked.\n
+Do you want to continue?`)
+ ) {
+ return false;
+ }*/
+ openProgressDialog('importingMacTags');
+
+ const entryCallback = (entry) => {
+ readMacOSTags(entry.path)
+ .then((tags) => {
+ if (tags.length > 0) {
+ addTags([entry], tags);
+ }
+ return tags;
+ })
+ .catch((err) => {
+ console.log('Error creating tags: ' + err);
+ });
+ };
+ Pro.MacTagsImport.importTags(
+ directoryPath,
+ currentLocation.listDirectoryPromise,
+ entryCallback,
+ recursive.current,
+ )
+ .then(() => {
+ closeProgressDialog();
+ console.log('Import tags succeeded ' + directoryPath);
+ showNotification(
+ 'Tags from ' + directoryPath + ' are imported successfully.',
+ 'default',
+ true,
+ );
+ return true;
+ })
+ .catch((err) => {
+ console.log('Error importing tags: ' + directoryPath, err);
+ closeProgressDialog();
+ });
+ } else {
+ showNotification(
+ t('core:thisFunctionalityIsAvailableInPro'),
+ 'default',
+ true,
+ );
+ return true;
+ }
+ }
+
+ return (
+ <Dialog
+ aria-labelledby="draggable-dialog-title"
+ PaperComponent={DraggablePaper}
+ open={open}
+ onClose={onClose}
+ style={{ minHeight: 200 }}
+ >
+ <DialogTitle
+ style={{ cursor: 'move', minHeight: 20 }}
+ data-tid="ImportMacTagDialogTitle"
+ id="draggable-dialog-title"
+ >
+ {t('core:ImportMacTags')}
+ <DialogCloseButton testId="closeImportMacTagTID" onClose={onClose} />
+ </DialogTitle>
+ <DialogContent>
+ <div style={{ margin: 20 }}>{t('core:importMacTagsHelp')}</div>
+ <FormControlLabel
+ labelPlacement="start"
+ control={
+ <Switch
+ data-tid="importTagsRecursiveTID"
+ checked={recursive.current}
+ onChange={toggleRecursive}
+ size="small"
+ name="importTagsRecursive"
+ />
+ }
+ label={t('core:importTagsRecursive')}
+ />
+ </DialogContent>
+ <TsDialogActions style={{ justifyContent: 'space-between' }}>
+ <TsButton
+ data-tid="cancelImportTagsTID"
+ onClick={() => {
+ onClose();
+ }}
+ >
+ {t('core:cancel')}
+ </TsButton>
+ <TsButton
+ data-tid="createTID"
+ variant="contained"
+ onClick={() => {
+ importMacTags();
+ }}
+ >
+ {t('core:ok')}
+ </TsButton>
+ </TsDialogActions>
+ </Dialog>
+ );
+}
+
+export default ImportMacTagDialog;
diff --git a/src/renderer/components/dialogs/hooks/ImportMacTagDialogContextProvider.tsx b/src/renderer/components/dialogs/hooks/ImportMacTagDialogContextProvider.tsx
new file mode 100644
index 000000000..b9508baa4
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/ImportMacTagDialogContextProvider.tsx
@@ -0,0 +1,97 @@
+/**
+ * TagSpaces - universal file and folder organizer
+ * Copyright (C) 2017-present TagSpaces GmbH
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License (version 3) as
+ * published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+import LoadingLazy from '-/components/LoadingLazy';
+import React, { createContext, useMemo, useReducer, useRef } from 'react';
+
+type ImportMacTagDialogContextData = {
+ openImportMacTagDialog: (directoryPath: string) => void;
+ closeImportMacTagDialog: () => void;
+};
+
+export const ImportMacTagDialogContext =
+ createContext<ImportMacTagDialogContextData>({
+ openImportMacTagDialog: () => {
+ throw new Error(
+ 'openImportMacTagDialog must be used within a ImportMacTagDialogContextProvider',
+ );
+ },
+ closeImportMacTagDialog: () => {
+ throw new Error(
+ 'closeImportMacTagDialog must be used within a ImportMacTagDialogContextProvider',
+ );
+ },
+ });
+
+export type ImportMacTagDialogContextProviderProps = {
+ children: React.ReactNode;
+};
+
+const ImportMacTagDialog = React.lazy(
+ () =>
+ import(
+ /* webpackChunkName: "ImportMacTagDialog" */ '../ImportMacTagDialog'
+ ),
+);
+
+export const ImportMacTagDialogContextProvider = ({
+ children,
+}: ImportMacTagDialogContextProviderProps) => {
+ const open = useRef<boolean>(false);
+ const directoryPath = useRef<string>(undefined);
+
+ const [ignored, forceUpdate] = useReducer((x) => x + 1, 0, undefined);
+
+ function openDialog(path) {
+ open.current = true;
+ directoryPath.current = path;
+ forceUpdate();
+ }
+
+ function closeDialog() {
+ open.current = false;
+ directoryPath.current = undefined;
+ forceUpdate();
+ }
+
+ function ImportMacTagDialogAsync(props) {
+ return (
+ <React.Suspense fallback={<LoadingLazy />}>
+ <ImportMacTagDialog {...props} />
+ </React.Suspense>
+ );
+ }
+
+ const context = useMemo(() => {
+ return {
+ openImportMacTagDialog: openDialog,
+ closeImportMacTagDialog: closeDialog,
+ };
+ }, []);
+
+ return (
+ <ImportMacTagDialogContext.Provider value={context}>
+ <ImportMacTagDialogAsync
+ open={open.current}
+ directoryPath={directoryPath.current}
+ onClose={closeDialog}
+ />
+ {children}
+ </ImportMacTagDialogContext.Provider>
+ );
+};
diff --git a/src/renderer/components/dialogs/hooks/useImportMacTagDialogContext.tsx b/src/renderer/components/dialogs/hooks/useImportMacTagDialogContext.tsx
new file mode 100644
index 000000000..c5bb761f0
--- /dev/null
+++ b/src/renderer/components/dialogs/hooks/useImportMacTagDialogContext.tsx
@@ -0,0 +1,6 @@
+import { useContext } from 'react';
+
+import { ImportMacTagDialogContext } from '-/components/dialogs/hooks/ImportMacTagDialogContextProvider';
+
+export const useImportMacTagDialogContext = () =>
+ useContext(ImportMacTagDialogContext);
diff --git a/src/renderer/components/menus/DirectoryMenu.tsx b/src/renderer/components/menus/DirectoryMenu.tsx
index 5821f248c..18c9f34d8 100644
--- a/src/renderer/components/menus/DirectoryMenu.tsx
+++ b/src/renderer/components/menus/DirectoryMenu.tsx
@@ -32,7 +32,6 @@ import {
createNewInstance,
getRelativeEntryPath,
openDirectoryMessage,
- readMacOSTags,
} from '-/services/utils-io';
import { PerspectiveIDs } from '-/perspectives';
import TsMenuList from '-/components/TsMenuList';
@@ -59,6 +58,7 @@ import { useDeleteMultipleEntriesDialogContext } from '-/components/dialogs/hook
import { useFileUploadContext } from '-/hooks/useFileUploadContext';
import { TabNames } from '-/hooks/EntryPropsTabsContextProvider';
import { useMenuContext } from '-/components/dialogs/hooks/useMenuContext';
+import { useImportMacTagDialogContext } from '-/components/dialogs/hooks/useImportMacTagDialogContext';
interface Props {
open: boolean;
@@ -106,6 +106,7 @@ function DirectoryMenu(props: Props) {
const { setReflectActions } = useEditedEntryContext();
const { openNewAudioDialog } = useNewAudioDialogContext();
const { openProTeaserDialog } = useProTeaserDialogContext();
+ const { openImportMacTagDialog } = useImportMacTagDialogContext();
const { openDeleteMultipleEntriesDialog } =
useDeleteMultipleEntriesDialogContext();
@@ -242,55 +243,7 @@ function DirectoryMenu(props: Props) {
}
function importMacTags() {
- if (Pro && Pro.MacTagsImport && Pro.MacTagsImport.importTags) {
- if (
- !confirm(`Experimental feature\n
-Depending on how many tags you have in your current directory, the tag extraction process may take a long time in which the application's user interface may appear as blocked.\n
-Do you want to continue?`)
- ) {
- return false;
- }
- openProgressDialog('importingMacTags');
-
- const entryCallback = (entry) => {
- readMacOSTags(entry.path)
- .then((tags) => {
- if (tags.length > 0) {
- addTags([entry], tags);
- }
- return tags;
- })
- .catch((err) => {
- console.log('Error creating tags: ' + err);
- });
- };
- Pro.MacTagsImport.importTags(
- directoryPath,
- currentLocation.listDirectoryPromise,
- entryCallback,
- )
- .then(() => {
- closeProgressDialog();
- console.log('Import tags succeeded ' + directoryPath);
- showNotification(
- 'Tags from ' + directoryPath + ' are imported successfully.',
- 'default',
- true,
- );
- return true;
- })
- .catch((err) => {
- console.log('Error importing tags: ' + directoryPath, err);
- closeProgressDialog();
- });
- } else {
- showNotification(
- t('core:thisFunctionalityIsAvailableInPro'),
- 'default',
- true,
- );
- return true;
- }
+ openImportMacTagDialog(directoryPath);
}
function onFail(message) {
diff --git a/src/renderer/containers/DialogsRoot.tsx b/src/renderer/containers/DialogsRoot.tsx
index 568d59857..979f33dec 100644
--- a/src/renderer/containers/DialogsRoot.tsx
+++ b/src/renderer/containers/DialogsRoot.tsx
@@ -37,6 +37,7 @@ import { ResolveConflictContextProvider } from '-/components/dialogs/hooks/Resol
import { DownloadUrlContextProvider } from '-/components/dialogs/hooks/DownloadUrlDialogContextProvider';
import { AiGenerationDialogContextProvider } from '-/components/dialogs/hooks/AiGenerationDialogContextProvider';
import { MenuContextProvider } from '-/components/dialogs/hooks/MenuContextProvider';
+import { ImportMacTagDialogContextProvider } from '-/components/dialogs/hooks/ImportMacTagDialogContextProvider';
export type DialogsRootProps = {
children: React.ReactNode;
@@ -63,9 +64,11 @@ function DialogsRoot({ children }: DialogsRootProps) {
<SettingsDialogContextProvider>
<ResolveConflictContextProvider>
<DownloadUrlContextProvider>
- <MenuContextProvider>
- {children}
- </MenuContextProvider>
+ <ImportMacTagDialogContextProvider>
+ <MenuContextProvider>
+ {children}
+ </MenuContextProvider>
+ </ImportMacTagDialogContextProvider>
</DownloadUrlContextProvider>
</ResolveConflictContextProvider>
</SettingsDialogContextProvider>
diff --git a/src/renderer/locales/en/core.json b/src/renderer/locales/en/core.json
index d689806f7..ec9fb4d48 100644
--- a/src/renderer/locales/en/core.json
+++ b/src/renderer/locales/en/core.json
@@ -546,6 +546,7 @@
"stopAll": "Stop all",
"fileExist": "File with the same name already exist",
"importMacTags": "Import macOS tags",
+ "importMacTagsHelp": "Experimental feature! Depending on how many tags you have in your current directory, the tag extraction process may take a long time in which the application's user interface may appear as blocked.",
"cantDownloadLocalFile": "You can't download local files",
"perspectiveSettingsTitle": "Perspective Settings",
"unlimited": "Unlimited",
@@ -974,6 +975,7 @@
"confirmReindex": "Index not available",
"confirmReindexContent": "Do you want to create it now?",
"importingMacTags": "Importing MacOS tags...",
+ "importTagsRecursive": "Importing tags recursive",
"noModelsLoaded": "Error: No AI-models are loaded, please check if the AI-service is running.",
"-": "-"
}