コード例 #1
0
func (c *Clipboard) CutToClipboard(files []string) {
	operations.CutToClipboard(files)
}
コード例 #2
0
// GenMenu generates json format menu content used in DeepinMenu for normal itself.
func (item *Item) GenMenu() (*Menu, error) {
	item.menu = NewMenu()
	item.files = make([]*gio.File, len(item.uris))
	for i, uri := range item.uris {
		item.files[i] = gio.FileNewForCommandlineArg(uri)
		if item.files[i] == nil {
			return nil, fmt.Errorf("No such a file or directory: %s", item.uri)
		}
	}

	menu := item.menu.AppendItem(NewMenuItem(Tr("_Open"), func(timestamp uint32) {
		activationPolicy := item.app.settings.ActivationPolicy()

		askingFiles := []string{}
		ops := []int32{}

		for _, itemURI := range item.uris {
			// FIXME: how to handle these errors.
			f := gio.FileNewForCommandlineArg(itemURI)
			if f == nil {
				continue
			}

			info, err := f.QueryInfo(strings.Join([]string{
				gio.FileAttributeAccessCanExecute,
				gio.FileAttributeStandardContentType,
			}, ","), gio.FileQueryInfoFlagsNone, nil)
			if err != nil {
				f.Unref()
				continue
			}

			isExecutable := info.GetAttributeBoolean(gio.FileAttributeAccessCanExecute)
			contentType := info.GetAttributeString(gio.FileAttributeStandardContentType)
			info.Unref()

			if isExecutable && isDesktopFile(itemURI) {
				item.app.activateDesktopFile(itemURI, []string{})
				continue
			}

			if activationPolicy == ActivationPolicyAsk && isExecutable && (contentTypeCanBeExecutable(contentType) || strings.HasSuffix(itemURI, ".bin")) {
				askingFiles = append(askingFiles, itemURI)
				ops = append(ops, OpOpen)
				f.Unref()
				continue
			}

			defaultApp, _ := getDefaultOpenApp(itemURI)
			if defaultApp == nil {
				askingFiles = append(askingFiles, itemURI)
				ops = append(ops, OpSelect)
				f.Unref()
				continue
			}
			defaultApp.Unref()

			item.app.doActivateFile(f, []string{}, isExecutable, contentType, timestamp, ActivateFlagRun)

			f.Unref()
		}

		if len(askingFiles) > 0 {
			item.app.emitRequestOpen(askingFiles, ops)
		}
	}, true))

	if containsSpecificItem(item.uris) {
		return menu, nil
	}

	// 1. multiple selection: not show "open with" if no possible open programmings.
	// 2. signle selection: show "open with" with "chose".
	// 3. desktop file should not has 'open with'.
	if item.shouldHasOpenWith() {
		possibleOpenProgrammings := getPossibleOpenProgramming(item.uris)
		if len(possibleOpenProgrammings) > 0 || !item.multiple {
			item.addOpenWithMenu(possibleOpenProgrammings)
		}
	}

	menu.AddSeparator()

	// TODO: use plugin, remove useless function.
	// disable app group right-button menu
	// isAppGroupItem := isAppGroup(filepath.Dir(item.uri))
	// if !isAppGroupItem {
	runFileRoller := func(cmd string, files []*gio.File, timestamp uint32) error {
		app, err := gio.AppInfoCreateFromCommandline(cmd, "", gio.AppInfoCreateFlagsSupportsStartupNotification)
		if err != nil {
			return err
		}
		defer app.Unref()
		_, err = app.Launch(files, gio.GetGdkAppLaunchContext().SetTimestamp(timestamp))
		return err
	}

	menu.AppendItem(NewMenuItem(Tr("Co_mpress"), func(timestamp uint32) {
		err := runFileRoller("file-roller -d %U", item.files, timestamp)
		if err != nil {
			Log.Error("run file-roller failed:", err)
		}
	}, true))

	allIsArchived := true
	for _, file := range item.files {
		if !isArchived(file) {
			allIsArchived = false
			break
		}
	}

	if allIsArchived {
		menu.AppendItem(NewMenuItem(Tr("_Extract Here"), func(timestamp uint32) {
			err := runFileRoller("file-roller -h", item.files, timestamp)
			if err != nil {
				Log.Error("run file-roller failed:", err)
			}
		}, true)).AddSeparator()
	}
	// }

	menu.AppendItem(NewMenuItem(Tr("Cu_t"), func(uint32) {
		operations.CutToClipboard(item.uris)
		item.app.emitItemCut(item.uris)
	}, true)).AppendItem(NewMenuItem(Tr("_Copy"), func(uint32) {
		operations.CopyToClipboard(item.uris)
		item.app.emitItemCopied(item.uris)
	}, true))

	// TODO: no design
	if !item.multiple {
		fileType := item.files[0].QueryFileType(gio.FileQueryInfoFlagsNone, nil)
		if fileType == gio.FileTypeDirectory {
			// menu.AppendItem(NewMenuItem(Tr("Paste _Into"), func(uint32) {
			// 	item.app.emitRequestPaste(item.uri)
			// }, operations.CanPaste(item.uri))).AddSeparator()
			if item.displayExtraItems {
				menu.AppendItem(NewMenuItem(Tr("Open in _terminal"), func(uint32) {
					runInTerminal(item.uri, "")
				}, true))
			}
		}
	}

	menu.AddSeparator()

	// if !isAppGroupItem {
	menu.AppendItem(NewMenuItem(Tr("_Rename"), func(uint32) {
		item.emitRequestRename()
	}, !item.multiple))
	// }

	menu.AppendItem(NewMenuItem(Tr("_Delete"), func(uint32) {
		item.emitRequestDelete()
	}, true))

	menu.AddSeparator()

	return item.menu.AppendItem(NewMenuItem(Tr("_Properties"), func(uint32) {
		item.showProperties()
	}, true)), nil
}