Compare commits
141 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 322c766ae5 | |||
| 9370bb4e02 | |||
| 68435e140d | |||
| cb2d0245a8 | |||
| 52567888ec | |||
| 09e6729e11 | |||
| e4f642318e | |||
| 2c36c39404 | |||
| 0e8f6735c5 | |||
| 111581ef02 | |||
| 0f247d55ab | |||
| d31a7dc172 | |||
| 1f4d35bcb1 | |||
| 10fc3155da | |||
| 0ce029de57 | |||
| 1953b152b4 | |||
| 3cd7a246ab | |||
| 6ff620d4ae | |||
|
|
93369900f8 | ||
| 7f5adf7962 | |||
| 0d38c8fafc | |||
| e2bf4d5f03 | |||
| b495b3e89f | |||
| df0b5e3e16 | |||
| 369278e734 | |||
| e3a840b051 | |||
| 841757857a | |||
| a9238ab3d1 | |||
| eb99c6fc43 | |||
| 89710d9d1a | |||
| f6ec802898 | |||
| 64fb9fbea0 | |||
| 46334687c9 | |||
| 8ec925aa8f | |||
| 48b0b7b1d8 | |||
| 05a62cb382 | |||
| d904b74629 | |||
| 7998ea33de | |||
| 1fec2983bd | |||
| e4bb5104e8 | |||
| 3e4621fe70 | |||
| 077ac81227 | |||
| e72bff4180 | |||
| e310879d61 | |||
| 03fbae7999 | |||
| da2c7f8e8b | |||
| dba15d43e1 | |||
| 5b749417c5 | |||
| d460b4ce11 | |||
| 244a58d8c2 | |||
| 89b231a2c9 | |||
| d14caf5269 | |||
| 9549b40745 | |||
| 01c241a01e | |||
| 7fef11ecd2 | |||
| c2bbaa26cf | |||
| 837333d8e2 | |||
| eeb19a5d2f | |||
| 23d3775d37 | |||
| 5f82c63aa7 | |||
| 812bf2fa73 | |||
| cbfa2e31ca | |||
| c817ea1392 | |||
| 1831486f34 | |||
| 5aef692b4f | |||
| 274aabe1f3 | |||
| 8739ccc55f | |||
| aaa6dc7e48 | |||
| 3b2ee6dfd4 | |||
| 41e2ae6131 | |||
| 5fc77f6b24 | |||
| dc457eb168 | |||
| 173c4cbddd | |||
| a25f92e4ec | |||
| 4254c80a64 | |||
| ca9cf51e3c | |||
| 29f4dc67b6 | |||
| a91b4859d0 | |||
| c89d6b945c | |||
| 5b6a342a9e | |||
| 8646a6338c | |||
| db16dcbb5d | |||
| 3af8b36940 | |||
| 9c6cf51825 | |||
| d58fd6e6f0 | |||
| 1487351f29 | |||
| e56c345156 | |||
| 37773383af | |||
| 8b0e59faa7 | |||
| 6ec455b515 | |||
| 4a86b4d2cf | |||
| 70270d60ba | |||
| bd70b8751a | |||
| 31fe08195f | |||
| c84512e3b3 | |||
| 9a11e35be0 | |||
| 7e8d465d81 | |||
| 526db2cbb7 | |||
| 5670b160d8 | |||
| ac41b053e3 | |||
| 37516306d3 | |||
| 71cee4019b | |||
| 94bcc12224 | |||
| 782abe2584 | |||
| 3e5777bf3a | |||
| c76c27288d | |||
| 98e8104602 | |||
| 95b49f41dd | |||
| b3b239d4c9 | |||
| c5afe976cf | |||
| f48c3ecfe4 | |||
| ce1383dc11 | |||
| 47cd43dba6 | |||
|
|
1be9ecb44e | ||
| 251a20e881 | |||
| c29693fbd0 | |||
| 9420af15a1 | |||
| 3008e4463b | |||
|
|
44fc047223 | ||
|
|
8348f34a4a | ||
| a4c8995299 | |||
| 0ff24002e5 | |||
| 7608d838aa | |||
| d432c2146b | |||
| 064d5a1032 | |||
| 39bc0cba24 | |||
| 07c5fa2d62 | |||
| 5c57551438 | |||
| 3c6aca04b4 | |||
| 9d572c8a6e | |||
| 680a75f5be | |||
| 4b0e3ba89a | |||
| 02f04d92f3 | |||
| a61b409486 | |||
| a7ba20ae00 | |||
| 9b43814846 | |||
| 8b8c9a669f | |||
| 7f538f4fae | |||
| 1d2bf37a44 | |||
| 2e16e0ae48 | |||
| 260e7ea01c |
17
.gitignore
vendored
17
.gitignore
vendored
@@ -1,8 +1,15 @@
|
|||||||
dist/
|
|
||||||
.cabal-sandbox/
|
|
||||||
cabal.sandbox.config
|
|
||||||
*~
|
|
||||||
*.hp
|
*.hp
|
||||||
*.prof
|
|
||||||
*.old
|
*.old
|
||||||
|
*.prof
|
||||||
|
*~
|
||||||
|
.cabal-sandbox/
|
||||||
|
.ghc.environment.*
|
||||||
.liquid/
|
.liquid/
|
||||||
|
.stack-work/
|
||||||
|
3rdparty/hpath
|
||||||
|
cabal.sandbox.config
|
||||||
|
dist-newstyle/
|
||||||
|
dist/
|
||||||
|
hscope.out
|
||||||
|
.ghcup
|
||||||
|
/bin/
|
||||||
|
|||||||
9
.gitmodules
vendored
9
.gitmodules
vendored
@@ -1,9 +0,0 @@
|
|||||||
[submodule "3rdparty/hpath"]
|
|
||||||
path = 3rdparty/hpath
|
|
||||||
url = https://github.com/hasufell/hpath.git
|
|
||||||
[submodule "3rdparty/hinotify"]
|
|
||||||
path = 3rdparty/hinotify
|
|
||||||
url = https://github.com/hasufell/hinotify.git
|
|
||||||
[submodule "3rdparty/simple-sendfile"]
|
|
||||||
path = 3rdparty/simple-sendfile
|
|
||||||
url = https://github.com/hasufell/simple-sendfile.git
|
|
||||||
68
.travis.yml
Normal file
68
.travis.yml
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
# See https://github.com/hvr/multi-ghc-travis for more information
|
||||||
|
|
||||||
|
language: c
|
||||||
|
|
||||||
|
sudo: required
|
||||||
|
dist: trusty
|
||||||
|
|
||||||
|
matrix:
|
||||||
|
include:
|
||||||
|
- env: CABALVER=1.24 GHCVER=8.0.1
|
||||||
|
addons: {apt: {packages: [cabal-install-1.24,ghc-8.0.1], sources: [hvr-ghc]}}
|
||||||
|
- env: CABALVER=2.0 GHCVER=8.2.2
|
||||||
|
addons: {apt: {packages: [cabal-install-2.0,ghc-8.2.2], sources: [hvr-ghc]}}
|
||||||
|
- env: CABALVER=2.2 GHCVER=8.4.1
|
||||||
|
addons: {apt: {packages: [cabal-install-2.2,ghc-8.4.1], sources: [hvr-ghc]}}
|
||||||
|
- env: CABALVER=head GHCVER=head
|
||||||
|
addons: {apt: {packages: [cabal-install-head,ghc-head,libgtk2.0-dev,libgtk-3-dev], sources: [hvr-ghc]}}
|
||||||
|
|
||||||
|
allow_failures:
|
||||||
|
- env: CABALVER=head GHCVER=head
|
||||||
|
|
||||||
|
env:
|
||||||
|
global:
|
||||||
|
- secure: "qAzj5tgAghFIfO6R/+Hdc5KcFhwXKNXMICNH7VLmqLzmYxk1UEkpi6hgX/f1bP5mLd07D+0IaeGFIUIWQOp+F/Du1NiX3yGbFuTt/Ja4I0K4ooCQc0w9uYLv8epxzp3VEOEI5sVCSpSomFjr7V0jwwTcBbxGUvv1VaGkJwAexRxCHuwU23KD0toECkVDsOMN/Gg2Ue/r2o+MsGx1/B9WMF0g6+zWlnrYfYZXWetl0DwATK5lZTa/21THdMrbuPX0fijGXTywvURDpCd3wIdfx9n7jPO2Gp2rcxPL/WkcIpzI211g4hEiheS+AlVyW39+C4i4MKaNK8YC+/5DRl/YHrFc7n3SZPDh+RMs6r3DS41RyRhQhz8DE0Pg4zfe/WUX4+h72TijCZ1zduh146rofwku/IGtCz5cuel+7cmTPk9ZyENYnH0ZMftkZjor9J/KamcMsN4zfaQBNJuIM3Kg8HVts3ymNIWrJ1LUn41MNt1eBDDvOWxZaHrjLyATRCFYvMr4RE01pqYKnWZ9RFfzVaYjD0QQWPWAXcCtkcAHSR6T0NxAqjLmHBNm+yWYIKG+bK2CvPNYTTNN8n4UvY1SrBpJEnLcRRns3U8nM7SVZ4GMaYzOTWtN1n0zamsl42wV0L/wqpz1SePkRZ34jca3V07XRLQSN2wjj8DyvOZUFR0="
|
||||||
|
|
||||||
|
before_install:
|
||||||
|
- sudo apt-get install -y hscolour
|
||||||
|
- export PATH=/opt/ghc/$GHCVER/bin:/opt/cabal/$CABALVER/bin:$PATH
|
||||||
|
|
||||||
|
install:
|
||||||
|
- cabal --version
|
||||||
|
- travis_retry cabal update
|
||||||
|
- cabal sandbox init
|
||||||
|
- cabal install alex happy
|
||||||
|
- export PATH="$(pwd)/.cabal-sandbox/bin:$PATH"
|
||||||
|
- cabal install gtk2hs-buildtools
|
||||||
|
- cabal install --only-dependencies --enable-tests -j
|
||||||
|
|
||||||
|
script:
|
||||||
|
- cabal configure --enable-tests -v2
|
||||||
|
- cabal build
|
||||||
|
- cabal test
|
||||||
|
- cabal check
|
||||||
|
- cabal sdist
|
||||||
|
# check that the generated source-distribution can be built & installed
|
||||||
|
- export SRC_TGZ=$(cabal info . | awk '{print $2 ".tar.gz";exit}') ;
|
||||||
|
cd dist/;
|
||||||
|
cabal sandbox init;
|
||||||
|
if [ -f "$SRC_TGZ" ]; then
|
||||||
|
cabal install alex happy;
|
||||||
|
export PATH="$(pwd)/.cabal-sandbox/bin:$PATH";
|
||||||
|
cabal install gtk2hs-buildtools;
|
||||||
|
cabal install "$SRC_TGZ" --enable-tests;
|
||||||
|
else
|
||||||
|
echo "expected '$SRC_TGZ' not found";
|
||||||
|
exit 1;
|
||||||
|
fi;
|
||||||
|
cd ..
|
||||||
|
- sed -i -e '/hsfm,/d' hsfm.cabal
|
||||||
|
- cabal haddock --executables --internal --hyperlink-source --html-location=https://hackage.haskell.org/package/\$pkg-\$version/docs/
|
||||||
|
|
||||||
|
after_script:
|
||||||
|
- ./update-gh-pages.sh
|
||||||
|
|
||||||
|
notifications:
|
||||||
|
email:
|
||||||
|
- hasufell@posteo.de
|
||||||
|
|
||||||
1
3rdparty/hinotify
vendored
1
3rdparty/hinotify
vendored
Submodule 3rdparty/hinotify deleted from 6751bf0cc8
1
3rdparty/hpath
vendored
1
3rdparty/hpath
vendored
Submodule 3rdparty/hpath deleted from a5360f29a3
1
3rdparty/simple-sendfile
vendored
1
3rdparty/simple-sendfile
vendored
Submodule 3rdparty/simple-sendfile deleted from 869c69d336
1
HACKING.md
Symbolic link
1
HACKING.md
Symbolic link
@@ -0,0 +1 @@
|
|||||||
|
hacking/HACKING.md
|
||||||
@@ -1,7 +1,8 @@
|
|||||||
HSFM
|
HSFM
|
||||||
====
|
====
|
||||||
|
|
||||||
__NOTE: This project is in a highly experimental state! Don't complain if it deletes your whole home directory. You should use a chroot, docker environment or similar for testing.__
|
[](https://gitter.im/hasufell/hsfm?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
|
||||||
|
[](https://travis-ci.org/hasufell/hsfm)
|
||||||
|
|
||||||
A Gtk+:3 filemanager written in Haskell.
|
A Gtk+:3 filemanager written in Haskell.
|
||||||
|
|
||||||
@@ -15,13 +16,13 @@ Design goals:
|
|||||||
Screenshots
|
Screenshots
|
||||||
-----------
|
-----------
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
Installation
|
Installation
|
||||||
------------
|
------------
|
||||||
|
|
||||||
```
|
```
|
||||||
cabal install
|
./install.sh
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
10
cabal.project
Normal file
10
cabal.project
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
with-compiler: ghc-8.6.5
|
||||||
|
|
||||||
|
packages: .
|
||||||
|
|
||||||
|
optimization: 2
|
||||||
|
|
||||||
|
package *
|
||||||
|
optimization: 2
|
||||||
|
|
||||||
|
index-state: 2020-01-24T20:23:40Z
|
||||||
80
cabal.project.freeze
Normal file
80
cabal.project.freeze
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
constraints: any.Cabal ==2.4.0.1,
|
||||||
|
any.IfElse ==0.85,
|
||||||
|
any.abstract-deque ==0.3,
|
||||||
|
abstract-deque -usecas,
|
||||||
|
any.alex ==3.2.5,
|
||||||
|
alex +small_base,
|
||||||
|
any.array ==0.5.3.0,
|
||||||
|
any.atomic-primops ==0.8.3,
|
||||||
|
atomic-primops -debug,
|
||||||
|
any.base ==4.12.0.0,
|
||||||
|
any.base-orphans ==0.8.1,
|
||||||
|
any.binary ==0.8.6.0,
|
||||||
|
any.bytestring ==0.10.8.2,
|
||||||
|
any.cairo ==0.13.8.0,
|
||||||
|
cairo +cairo_pdf +cairo_ps +cairo_svg,
|
||||||
|
any.containers ==0.6.0.1,
|
||||||
|
any.deepseq ==1.4.4.0,
|
||||||
|
any.directory ==1.3.3.0,
|
||||||
|
any.exceptions ==0.10.4,
|
||||||
|
exceptions +transformers-0-4,
|
||||||
|
any.filepath ==1.4.2.1,
|
||||||
|
any.ghc-boot-th ==8.6.5,
|
||||||
|
any.ghc-prim ==0.5.3,
|
||||||
|
any.gio ==0.13.8.0,
|
||||||
|
any.glib ==0.13.8.0,
|
||||||
|
glib +closure_signals,
|
||||||
|
any.gtk2hs-buildtools ==0.13.8.0,
|
||||||
|
gtk2hs-buildtools +closuresignals,
|
||||||
|
any.gtk3 ==0.15.4,
|
||||||
|
gtk3 -build-demos +fmode-binary +have-gio,
|
||||||
|
any.happy ==1.19.12,
|
||||||
|
happy +small_base,
|
||||||
|
any.hashable ==1.3.0.0,
|
||||||
|
hashable -examples +integer-gmp +sse2 -sse41,
|
||||||
|
any.hashtables ==1.2.3.4,
|
||||||
|
hashtables -bounds-checking -debug -detailed-profiling -portable -sse42 +unsafe-tricks,
|
||||||
|
any.heaps ==0.3.6.1,
|
||||||
|
any.hinotify-bytestring ==0.3.8.1,
|
||||||
|
any.hpath ==0.11.0,
|
||||||
|
any.hpath-filepath ==0.10.3,
|
||||||
|
any.hpath-io ==0.12.0,
|
||||||
|
any.hsc2hs ==0.68.6,
|
||||||
|
hsc2hs -in-ghc-tree,
|
||||||
|
any.integer-gmp ==1.0.2.0,
|
||||||
|
any.lockfree-queue ==0.2.3.1,
|
||||||
|
any.monad-control ==1.0.2.3,
|
||||||
|
any.monad-loops ==0.4.3,
|
||||||
|
monad-loops +base4,
|
||||||
|
any.mtl ==2.2.2,
|
||||||
|
any.network ==3.1.1.1,
|
||||||
|
any.old-locale ==1.0.0.7,
|
||||||
|
any.pango ==0.13.8.0,
|
||||||
|
pango +new-exception,
|
||||||
|
any.parsec ==3.1.13.0,
|
||||||
|
any.pretty ==1.1.3.6,
|
||||||
|
any.primitive ==0.7.0.0,
|
||||||
|
any.process ==1.6.5.0,
|
||||||
|
any.random ==1.1,
|
||||||
|
any.rts ==1.0,
|
||||||
|
any.safe ==0.3.18,
|
||||||
|
any.safe-exceptions ==0.1.7.0,
|
||||||
|
any.simple-sendfile ==0.2.30,
|
||||||
|
simple-sendfile +allow-bsd,
|
||||||
|
any.stm ==2.5.0.0,
|
||||||
|
any.streamly ==0.7.0,
|
||||||
|
streamly -benchmark -debug -dev -examples -examples-sdl -has-llvm -inspection -no-charts -no-fusion -streamk,
|
||||||
|
any.template-haskell ==2.14.0.0,
|
||||||
|
any.text ==1.2.3.1,
|
||||||
|
any.time ==1.8.0.2,
|
||||||
|
any.transformers ==0.5.6.2,
|
||||||
|
any.transformers-base ==0.4.5.2,
|
||||||
|
transformers-base +orphaninstances,
|
||||||
|
any.transformers-compat ==0.6.5,
|
||||||
|
transformers-compat -five +five-three -four +generic-deriving +mtl -three -two,
|
||||||
|
any.unix ==2.7.2.2,
|
||||||
|
any.unix-bytestring ==0.3.7.3,
|
||||||
|
any.utf8-string ==1.0.1.1,
|
||||||
|
any.vector ==0.12.0.3,
|
||||||
|
vector +boundschecks -internalchecks -unsafechecks -wall,
|
||||||
|
any.word8 ==0.1.3
|
||||||
1021
data/Gtk/builder.xml
1021
data/Gtk/builder.xml
File diff suppressed because it is too large
Load Diff
@@ -1,37 +1,23 @@
|
|||||||
HACKING
|
# HACKING
|
||||||
=======
|
|
||||||
|
|
||||||
Check out the [issue tracker](https://github.com/hasufell/hsfm/issues)
|
Check out the [issue tracker](https://github.com/hasufell/hsfm/issues)
|
||||||
if you don't know yet what you want to hack on.
|
if you don't know yet what you want to hack on.
|
||||||
|
|
||||||
Coding style
|
## Coding style
|
||||||
------------
|
|
||||||
|
|
||||||
- match the sorroundings
|
- match the sorroundings
|
||||||
- no overcomplicated pointfree style
|
- no overcomplicated pointfree style
|
||||||
- normal indenting 2 whitespaces
|
- normal indenting 2 whitespaces
|
||||||
- just make things pretty and readable
|
- just make things pretty and readable
|
||||||
- use the provided [hsimport.hs](hsimport.hs)
|
- you can use the provided [hsimport.hs](hsimport.hs)
|
||||||
|
|
||||||
Documentation
|
## Documentation
|
||||||
-------------
|
|
||||||
|
|
||||||
__Everything__ must be documented. :)
|
__Everything__ must be documented. :)
|
||||||
|
Don't assume people know what you mean. Type signatures are not sufficient
|
||||||
|
documentation.
|
||||||
|
|
||||||
Hacking Guide
|
## Hacking Overview
|
||||||
-------------
|
|
||||||
|
|
||||||
The main data structure for the IO related File type is in
|
|
||||||
[HSFM.FileSystem.FileType](./../src/HSFM/FileSystem/FileType.hs#L93), which
|
|
||||||
should be seen as a library. This is the entry point where
|
|
||||||
[directory contents are read](./../src/HSFM/FileSystem/FileType.hs#L465)
|
|
||||||
and the File type in general [is constructed](./../src/HSFM/FileSystem/FileType.hs#L302).
|
|
||||||
The File type uses a safe Path type under the hood instead of Strings,
|
|
||||||
utilizing the [hpath](https://github.com/hasufell/hpath) library.
|
|
||||||
|
|
||||||
File operations (like copy, delete etc) are defined at
|
|
||||||
[HSFM.FileSystem.FileOperation](./../src/HSFM/FileSystem/FileOperations.hs)
|
|
||||||
which use this File type.
|
|
||||||
|
|
||||||
Only a GTK GUI is currently implemented, the entry point being
|
Only a GTK GUI is currently implemented, the entry point being
|
||||||
[HSFM.GUI.Gtk](./../src/HSFM/GUI/Gtk.hs). From there it flows down
|
[HSFM.GUI.Gtk](./../src/HSFM/GUI/Gtk.hs). From there it flows down
|
||||||
@@ -52,3 +38,68 @@ following files:
|
|||||||
* [HSFM.GUI.Gtk.MyGUI](./../src/HSFM/GUI/Gtk/MyGUI.hs): add initializers for the GUI buttons to be fetched from the GTK builder.xml file
|
* [HSFM.GUI.Gtk.MyGUI](./../src/HSFM/GUI/Gtk/MyGUI.hs): add initializers for the GUI buttons to be fetched from the GTK builder.xml file
|
||||||
* [HSFM.GUI.Gtk.Callbacks](./../src/HSFM/GUI/Gtk/Callbacks.hs): define the callbacks and the actual functionality here
|
* [HSFM.GUI.Gtk.Callbacks](./../src/HSFM/GUI/Gtk/Callbacks.hs): define the callbacks and the actual functionality here
|
||||||
|
|
||||||
|
## Concepts
|
||||||
|
|
||||||
|
### Path safety
|
||||||
|
|
||||||
|
Paths are usually represented in haskell libraries as `type FilePath = String`.
|
||||||
|
This is bad, because of a number of reasons:
|
||||||
|
* encoding issues, since the low-level representation of filepaths is in fact an array of C chars
|
||||||
|
* weak typing... we could pass arbitrary invalid/malicious filepaths or other random strings
|
||||||
|
* no information about any property at type level (e.g. is it an absolute path?)
|
||||||
|
* no filepath constructors that do sanity checks and proper parsing
|
||||||
|
* no guarantee whether the filepath is normalised or not or even valid
|
||||||
|
|
||||||
|
Because of that, the solution is:
|
||||||
|
* use `ByteString` under the hood
|
||||||
|
* wrap it inside `Path t` where `t` can be either `Abs` (for absolute), `Rel` (for relative) or `Fn` (for filename)
|
||||||
|
* construct filepaths via smart constructors only that reject certain paths (like `.` or `..`) and normalise the path
|
||||||
|
|
||||||
|
This leads to the following benefits:
|
||||||
|
* we have guarantees about whether a path is absolute or not, which is important for runtime safety in general, predictable behavior and thread safety
|
||||||
|
* we don't mess with the filepath representation we get from low-level posix functions, so encoding issues are pretty much out
|
||||||
|
* we can reason about filepaths and rely on them to be valid (don't confuse that with "they exist")
|
||||||
|
* filepath functions like `(</>)` are now predictable and safe in contrast to the version from the `filepath` package
|
||||||
|
|
||||||
|
The [hpath](https://hackage.haskell.org/package/hpath) library does exactly that for us.
|
||||||
|
|
||||||
|
The only problem with this approach is that most libraries are still String
|
||||||
|
based. Some provide dedicated `Foo.ByteString` modules though, but it
|
||||||
|
might be necessary to fork libraries.
|
||||||
|
We also need to keep track of the [Abstract FilePath proposal](https://ghc.haskell.org/trac/ghc/wiki/Proposal/AbstractFilePath).
|
||||||
|
|
||||||
|
Almost all paths in HSFM are only allowed to be absolute (`Path Abs`), unless
|
||||||
|
they are filenames (`Path Fn`) and processed for GUI purposes. This is as
|
||||||
|
already mentioned for the purpose of runtime safety, predictability and
|
||||||
|
thread safety.
|
||||||
|
|
||||||
|
### File IO safety
|
||||||
|
|
||||||
|
This is a pretty difficult problem. One thing to ensure safety on IO level
|
||||||
|
is simply the strong haskell type system, since we push everything
|
||||||
|
into our `File a` type and can then pattern match easily against the different
|
||||||
|
types of files.
|
||||||
|
|
||||||
|
The only problem with this approach is that we are examining a file at point
|
||||||
|
`a` in time, safe the information and then use that information further down
|
||||||
|
the call stack at point `b` in time, when the file information in memory
|
||||||
|
could already be out of date. There are two approaches to make this less
|
||||||
|
sucky:
|
||||||
|
* use the hinotify library on GUI level to refresh the view (and the File representation in memory) whenever the contents of a directory changes
|
||||||
|
* when we stuff something into the copy buffer, it is not saved as type `File a`, but as `Path Abs`... when the operation is finalized then the file at the given path is read and the copy/move/whatnot function carried out immediately
|
||||||
|
|
||||||
|
In addition, we don't use the `directory` package, which is dangerous
|
||||||
|
and broken. Instead, we use the [HPath.IO](https://hackage.haskell.org/package/hpath/docs/HPath-IO.html).
|
||||||
|
|
||||||
|
### Exception handling
|
||||||
|
|
||||||
|
Exceptions are good. We don't want to wrap everything in Maybe/Either types
|
||||||
|
unless we want to handle failure immediately. Otherwise we need to make
|
||||||
|
sure that at least at some point IOExceptions are caught and visualized
|
||||||
|
to the user. This is often done via e.g. `withErrorDialog` which catches
|
||||||
|
`IOException` and [HPathIOException](https://hackage.haskell.org/package/hpath/docs/HPath-IO-Errors.html#t:HPathIOException).
|
||||||
|
|
||||||
|
It's also important to clean up stuff like filedescriptors via
|
||||||
|
functions like `bracket` directly in our low-level code in case
|
||||||
|
something goes wrong.
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ main = hsimport $ defaultConfig { prettyPrint = prettyPrint
|
|||||||
where
|
where
|
||||||
prettyPrint :: HS.ImportDecl -> String
|
prettyPrint :: HS.ImportDecl -> String
|
||||||
prettyPrint (HS.ImportDecl sloc modname qual _ _ mpkg mas mspec) =
|
prettyPrint (HS.ImportDecl sloc modname qual _ _ mpkg mas mspec) =
|
||||||
"import " ++ (ifStr qual "qualified") ++
|
"import " ++ (ifStr qual "qualified ") ++
|
||||||
(maybe "" (\pkg -> " \"" ++ pkg ++ "\" ") mpkg) ++
|
(maybe "" (\pkg -> " \"" ++ pkg ++ "\" ") mpkg) ++
|
||||||
getMN modname ++ (maybe "" (\name -> " as " ++ getMN name) $ mas) ++
|
getMN modname ++ (maybe "" (\name -> " as " ++ getMN name) $ mas) ++
|
||||||
specprint mspec
|
specprint mspec
|
||||||
@@ -16,9 +16,9 @@ main = hsimport $ defaultConfig { prettyPrint = prettyPrint
|
|||||||
specprint :: Maybe (Bool, [HS.ImportSpec]) -> String
|
specprint :: Maybe (Bool, [HS.ImportSpec]) -> String
|
||||||
specprint Nothing = ""
|
specprint Nothing = ""
|
||||||
specprint (Just (False, xs))
|
specprint (Just (False, xs))
|
||||||
= "\n (\n" ++ printImportSpecs xs ++ "\n )"
|
= "\n (\n" ++ printImportSpecs xs ++ " )"
|
||||||
specprint (Just (True, xs))
|
specprint (Just (True, xs))
|
||||||
= "\n hiding (\n" ++ printImportSpecs xs ++ "\n )"
|
= "\n hiding (\n" ++ printImportSpecs xs ++ " )"
|
||||||
|
|
||||||
printImportSpecs :: [HS.ImportSpec] -> String
|
printImportSpecs :: [HS.ImportSpec] -> String
|
||||||
printImportSpecs ins
|
printImportSpecs ins
|
||||||
@@ -26,7 +26,7 @@ main = hsimport $ defaultConfig { prettyPrint = prettyPrint
|
|||||||
in " " ++ printSpec x ++ "\n" ++ go xs
|
in " " ++ printSpec x ++ "\n" ++ go xs
|
||||||
where
|
where
|
||||||
go [] = ""
|
go [] = ""
|
||||||
go [x'] = " , " ++ printSpec x'
|
go [x'] = " , " ++ printSpec x' ++ "\n"
|
||||||
go (x':xs') = " , " ++ printSpec x' ++ "\n" ++ go xs'
|
go (x':xs') = " , " ++ printSpec x' ++ "\n" ++ go xs'
|
||||||
printSpec :: HS.ImportSpec -> String
|
printSpec :: HS.ImportSpec -> String
|
||||||
printSpec = HS.prettyPrint
|
printSpec = HS.prettyPrint
|
||||||
|
|||||||
58
hsfm.cabal
58
hsfm.cabal
@@ -10,7 +10,7 @@ copyright: Copyright: (c) 2016 Julian Ospald
|
|||||||
homepage: https://github.com/hasufell/hsfm
|
homepage: https://github.com/hasufell/hsfm
|
||||||
category: Desktop
|
category: Desktop
|
||||||
build-type: Simple
|
build-type: Simple
|
||||||
cabal-version: >=1.10
|
cabal-version: >=1.22
|
||||||
|
|
||||||
data-files:
|
data-files:
|
||||||
LICENSE
|
LICENSE
|
||||||
@@ -24,30 +24,26 @@ data-files:
|
|||||||
|
|
||||||
library
|
library
|
||||||
exposed-modules:
|
exposed-modules:
|
||||||
HSFM.FileSystem.Errors
|
|
||||||
HSFM.FileSystem.FileOperations
|
|
||||||
HSFM.FileSystem.FileType
|
HSFM.FileSystem.FileType
|
||||||
|
HSFM.FileSystem.UtilTypes
|
||||||
|
HSFM.History
|
||||||
|
HSFM.Settings
|
||||||
HSFM.Utils.IO
|
HSFM.Utils.IO
|
||||||
HSFM.Utils.MyPrelude
|
HSFM.Utils.MyPrelude
|
||||||
|
|
||||||
build-depends:
|
build-depends:
|
||||||
base >= 4.7,
|
IfElse,
|
||||||
|
base >= 4.8 && < 5,
|
||||||
bytestring,
|
bytestring,
|
||||||
containers,
|
|
||||||
data-default,
|
|
||||||
filepath >= 1.3.0.0,
|
filepath >= 1.3.0.0,
|
||||||
hinotify,
|
hinotify-bytestring,
|
||||||
hpath,
|
hpath >= 0.11.0 ,
|
||||||
mtl >= 2.2,
|
hpath-filepath >= 0.10.3,
|
||||||
old-locale >= 1,
|
hpath-io >= 0.12.0,
|
||||||
posix-paths,
|
|
||||||
process,
|
|
||||||
safe,
|
safe,
|
||||||
simple-sendfile,
|
|
||||||
stm,
|
stm,
|
||||||
time >= 1.4.2,
|
time >= 1.4.2,
|
||||||
unix,
|
unix,
|
||||||
unix-bytestring,
|
|
||||||
utf8-string
|
utf8-string
|
||||||
hs-source-dirs: src
|
hs-source-dirs: src
|
||||||
default-language: Haskell2010
|
default-language: Haskell2010
|
||||||
@@ -56,40 +52,46 @@ library
|
|||||||
FlexibleInstances
|
FlexibleInstances
|
||||||
ViewPatterns
|
ViewPatterns
|
||||||
ghc-options:
|
ghc-options:
|
||||||
-O2
|
|
||||||
-threaded
|
|
||||||
-Wall
|
-Wall
|
||||||
"-with-rtsopts=-N"
|
|
||||||
|
|
||||||
executable hsfm-gtk
|
executable hsfm-gtk
|
||||||
main-is: HSFM/GUI/Gtk.hs
|
main-is: HSFM/GUI/Gtk.hs
|
||||||
other-modules:
|
other-modules:
|
||||||
|
Paths_hsfm
|
||||||
|
HSFM.FileSystem.FileType
|
||||||
|
HSFM.FileSystem.UtilTypes
|
||||||
HSFM.GUI.Glib.GlibString
|
HSFM.GUI.Glib.GlibString
|
||||||
HSFM.GUI.Gtk.Callbacks
|
HSFM.GUI.Gtk.Callbacks
|
||||||
|
HSFM.GUI.Gtk.Callbacks.Utils
|
||||||
HSFM.GUI.Gtk.Data
|
HSFM.GUI.Gtk.Data
|
||||||
HSFM.GUI.Gtk.Dialogs
|
HSFM.GUI.Gtk.Dialogs
|
||||||
HSFM.GUI.Gtk.Errors
|
HSFM.GUI.Gtk.Errors
|
||||||
HSFM.GUI.Gtk.Icons
|
HSFM.GUI.Gtk.Icons
|
||||||
HSFM.GUI.Gtk.MyGUI
|
HSFM.GUI.Gtk.MyGUI
|
||||||
HSFM.GUI.Gtk.MyView
|
HSFM.GUI.Gtk.MyView
|
||||||
|
HSFM.GUI.Gtk.Plugins
|
||||||
|
HSFM.GUI.Gtk.Settings
|
||||||
HSFM.GUI.Gtk.Utils
|
HSFM.GUI.Gtk.Utils
|
||||||
|
HSFM.History
|
||||||
|
HSFM.Settings
|
||||||
|
HSFM.Utils.IO
|
||||||
HSFM.Utils.MyPrelude
|
HSFM.Utils.MyPrelude
|
||||||
|
|
||||||
build-depends:
|
build-depends:
|
||||||
Cabal >= 1.22.0.0,
|
Cabal >= 1.22.0.0,
|
||||||
base >= 4.7,
|
IfElse,
|
||||||
|
base >= 4.8 && < 5,
|
||||||
bytestring,
|
bytestring,
|
||||||
containers,
|
|
||||||
data-default,
|
|
||||||
filepath >= 1.3.0.0,
|
filepath >= 1.3.0.0,
|
||||||
glib >= 0.13,
|
glib >= 0.13,
|
||||||
gtk3 >= 0.14.1,
|
gtk3 >= 0.14.1,
|
||||||
hinotify,
|
hinotify-bytestring,
|
||||||
hpath,
|
hpath >= 0.11.0 ,
|
||||||
|
hpath-filepath >= 0.10.3,
|
||||||
|
hpath-io >= 0.12.0,
|
||||||
hsfm,
|
hsfm,
|
||||||
mtl >= 2.2,
|
monad-loops,
|
||||||
old-locale >= 1,
|
old-locale >= 1,
|
||||||
posix-paths,
|
|
||||||
process,
|
process,
|
||||||
safe,
|
safe,
|
||||||
simple-sendfile,
|
simple-sendfile,
|
||||||
@@ -107,7 +109,9 @@ executable hsfm-gtk
|
|||||||
FlexibleInstances
|
FlexibleInstances
|
||||||
ViewPatterns
|
ViewPatterns
|
||||||
ghc-options:
|
ghc-options:
|
||||||
-O2
|
|
||||||
-threaded
|
|
||||||
-Wall
|
-Wall
|
||||||
"-with-rtsopts=-N"
|
|
||||||
|
source-repository head
|
||||||
|
type: git
|
||||||
|
location: https://github.com/hasufell/hsfm
|
||||||
|
|
||||||
|
|||||||
42
install.sh
Executable file
42
install.sh
Executable file
@@ -0,0 +1,42 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(CDPATH="" cd -- "$(dirname -- "$0")" && pwd -P)"
|
||||||
|
|
||||||
|
cd "${SCRIPT_DIR}"
|
||||||
|
|
||||||
|
# install ghcup
|
||||||
|
if ! [ -e "${SCRIPT_DIR}"/.ghcup/bin/ghcup ] ; then
|
||||||
|
mkdir -p "${SCRIPT_DIR}"/.ghcup/bin
|
||||||
|
curl --proto '=https' --tlsv1.2 -sSf https://gitlab.haskell.org/haskell/ghcup/raw/master/ghcup > "${SCRIPT_DIR}"/.ghcup/bin/ghcup
|
||||||
|
chmod +x "${SCRIPT_DIR}"/.ghcup/bin/ghcup
|
||||||
|
fi
|
||||||
|
|
||||||
|
# set up environment
|
||||||
|
export PATH="${SCRIPT_DIR}/.ghcup/bin:$PATH"
|
||||||
|
export GHCUP_INSTALL_BASE_PREFIX="${SCRIPT_DIR}"
|
||||||
|
|
||||||
|
# get ghc version from cabal.project
|
||||||
|
ghc_ver=$(grep with-compiler cabal.project | awk '{print $2}' | sed 's/ghc-//')
|
||||||
|
|
||||||
|
# install ghc
|
||||||
|
if ! ghcup list -t ghc -c installed -r | grep -q "${ghc_ver}" ; then
|
||||||
|
ghcup install "${ghc_ver}"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# install cabal-install
|
||||||
|
if [ -z "$(ghcup list -t cabal-install -c installed -r)" ] ; then
|
||||||
|
ghcup install-cabal
|
||||||
|
fi
|
||||||
|
|
||||||
|
[ -e "${SCRIPT_DIR}"/bin ] || mkdir "${SCRIPT_DIR}"/bin
|
||||||
|
|
||||||
|
# install binary
|
||||||
|
cabal v2-install \
|
||||||
|
--installdir="${SCRIPT_DIR}"/bin \
|
||||||
|
--install-method=copy \
|
||||||
|
--overwrite-policy=always
|
||||||
|
|
||||||
|
echo "Binary installed in: ${SCRIPT_DIR}/bin"
|
||||||
|
|
||||||
@@ -1,251 +0,0 @@
|
|||||||
{--
|
|
||||||
HSFM, a filemanager written in Haskell.
|
|
||||||
Copyright (C) 2016 Julian Ospald
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU General Public License
|
|
||||||
version 2 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 General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
--}
|
|
||||||
|
|
||||||
{-# LANGUAGE DeriveDataTypeable #-}
|
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
||||||
|
|
||||||
-- |Provides error handling.
|
|
||||||
module HSFM.FileSystem.Errors where
|
|
||||||
|
|
||||||
|
|
||||||
import Control.Exception
|
|
||||||
import Control.Monad
|
|
||||||
(
|
|
||||||
when
|
|
||||||
, forM
|
|
||||||
)
|
|
||||||
import Data.ByteString
|
|
||||||
(
|
|
||||||
ByteString
|
|
||||||
)
|
|
||||||
import Data.Typeable
|
|
||||||
import Foreign.C.Error
|
|
||||||
(
|
|
||||||
getErrno
|
|
||||||
, Errno
|
|
||||||
)
|
|
||||||
import qualified HPath as P
|
|
||||||
import HPath
|
|
||||||
(
|
|
||||||
Abs
|
|
||||||
, Path
|
|
||||||
)
|
|
||||||
import HSFM.Utils.IO
|
|
||||||
import System.IO.Error
|
|
||||||
(
|
|
||||||
catchIOError
|
|
||||||
)
|
|
||||||
|
|
||||||
import qualified System.Posix.Directory.ByteString as PFD
|
|
||||||
import System.Posix.FilePath
|
|
||||||
import qualified System.Posix.Files.ByteString as PF
|
|
||||||
|
|
||||||
|
|
||||||
data FmIOException = FileDoesNotExist ByteString
|
|
||||||
| DirDoesNotExist ByteString
|
|
||||||
| PathNotAbsolute ByteString
|
|
||||||
| FileNotExecutable ByteString
|
|
||||||
| SameFile ByteString ByteString
|
|
||||||
| NotAFile ByteString
|
|
||||||
| NotADir ByteString
|
|
||||||
| DestinationInSource ByteString ByteString
|
|
||||||
| FileDoesExist ByteString
|
|
||||||
| DirDoesExist ByteString
|
|
||||||
| IsSymlink ByteString
|
|
||||||
| InvalidOperation String
|
|
||||||
| InvalidFileName
|
|
||||||
| Can'tOpenDirectory ByteString
|
|
||||||
| CopyFailed String
|
|
||||||
| MoveFailed String
|
|
||||||
deriving (Typeable)
|
|
||||||
|
|
||||||
|
|
||||||
instance Show FmIOException where
|
|
||||||
show (FileDoesNotExist fp) = "File does not exist:" ++ P.fpToString fp
|
|
||||||
show (DirDoesNotExist fp) = "Directory does not exist: "
|
|
||||||
++ P.fpToString fp
|
|
||||||
show (PathNotAbsolute fp) = "Path not absolute: " ++ P.fpToString fp
|
|
||||||
show (FileNotExecutable fp) = "File not executable: "
|
|
||||||
++ P.fpToString fp
|
|
||||||
show (SameFile fp1 fp2) = P.fpToString fp1
|
|
||||||
++ " and " ++ P.fpToString fp2
|
|
||||||
++ " are the same file!"
|
|
||||||
show (NotAFile fp) = "Not a file: " ++ P.fpToString fp
|
|
||||||
show (NotADir fp) = "Not a directory: " ++ P.fpToString fp
|
|
||||||
show (DestinationInSource fp1 fp2) = P.fpToString fp1
|
|
||||||
++ " is contained in "
|
|
||||||
++ P.fpToString fp2
|
|
||||||
show (FileDoesExist fp) = "File does exist: " ++ P.fpToString fp
|
|
||||||
show (DirDoesExist fp) = "Directory does exist: " ++ P.fpToString fp
|
|
||||||
show (IsSymlink fp) = "Is a symlink: " ++ P.fpToString fp
|
|
||||||
show (InvalidOperation str) = "Invalid operation: " ++ str
|
|
||||||
show InvalidFileName = "Invalid file name!"
|
|
||||||
show (Can'tOpenDirectory fp) = "Can't open directory: "
|
|
||||||
++ P.fpToString fp
|
|
||||||
show (CopyFailed str) = "Copying failed: " ++ str
|
|
||||||
show (MoveFailed str) = "Moving failed: " ++ str
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
instance Exception FmIOException
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----------------------------
|
|
||||||
--[ Path based functions ]--
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
|
|
||||||
throwFileDoesExist :: Path Abs -> IO ()
|
|
||||||
throwFileDoesExist fp =
|
|
||||||
whenM (doesFileExist fp) (throw . FileDoesExist
|
|
||||||
. P.fromAbs $ fp)
|
|
||||||
|
|
||||||
|
|
||||||
throwDirDoesExist :: Path Abs -> IO ()
|
|
||||||
throwDirDoesExist fp =
|
|
||||||
whenM (doesDirectoryExist fp) (throw . DirDoesExist
|
|
||||||
. P.fromAbs $ fp)
|
|
||||||
|
|
||||||
|
|
||||||
throwFileDoesNotExist :: Path Abs -> IO ()
|
|
||||||
throwFileDoesNotExist fp =
|
|
||||||
whenM (doesFileExist fp) (throw . FileDoesExist
|
|
||||||
. P.fromAbs $ fp)
|
|
||||||
|
|
||||||
|
|
||||||
throwDirDoesNotExist :: Path Abs -> IO ()
|
|
||||||
throwDirDoesNotExist fp =
|
|
||||||
whenM (doesDirectoryExist fp) (throw . DirDoesExist
|
|
||||||
. P.fromAbs $ fp)
|
|
||||||
|
|
||||||
|
|
||||||
throwSameFile :: Path Abs -- ^ will be canonicalized
|
|
||||||
-> Path Abs -- ^ will be canonicalized
|
|
||||||
-> IO ()
|
|
||||||
throwSameFile fp1 fp2 = do
|
|
||||||
fp1' <- fmap P.fromAbs $ P.canonicalizePath fp1
|
|
||||||
-- TODO: clean this up... if canonicalizing fp2 fails we try to
|
|
||||||
-- canonicalize `dirname fp2`
|
|
||||||
fp2' <- catchIOError (fmap P.fromAbs $ P.canonicalizePath fp2)
|
|
||||||
(\_ -> fmap P.fromAbs
|
|
||||||
$ (\x -> maybe x (\y -> x P.</> y) $ P.basename fp2)
|
|
||||||
<$> (P.canonicalizePath $ P.dirname fp2))
|
|
||||||
when (equalFilePath fp1' fp2') (throw $ SameFile fp1' fp2')
|
|
||||||
|
|
||||||
|
|
||||||
-- |Checks whether the destination directory is contained
|
|
||||||
-- within the source directory by comparing the device+file ID of the
|
|
||||||
-- source directory with all device+file IDs of the parent directories
|
|
||||||
-- of the destination.
|
|
||||||
throwDestinationInSource :: Path Abs -- ^ source dir
|
|
||||||
-> Path Abs -- ^ full destination, `dirname dest`
|
|
||||||
-- must exist
|
|
||||||
-> IO ()
|
|
||||||
throwDestinationInSource source dest = do
|
|
||||||
source' <- P.canonicalizePath source
|
|
||||||
dest' <- (\x -> maybe x (\y -> x P.</> y) $ P.basename dest)
|
|
||||||
<$> (P.canonicalizePath $ P.dirname dest)
|
|
||||||
dids <- forM (P.getAllParents dest') $ \p -> do
|
|
||||||
fs <- PF.getSymbolicLinkStatus (P.fromAbs p)
|
|
||||||
return (PF.deviceID fs, PF.fileID fs)
|
|
||||||
sid <- fmap (\x -> (PF.deviceID x, PF.fileID x))
|
|
||||||
$ PF.getSymbolicLinkStatus (P.fromAbs source')
|
|
||||||
when (elem sid dids)
|
|
||||||
(throw $ DestinationInSource (P.fromAbs dest)
|
|
||||||
(P.fromAbs source))
|
|
||||||
|
|
||||||
|
|
||||||
-- |Checks if the given file exists and is not a directory. This follows
|
|
||||||
-- symlinks, but will return True if the symlink is broken.
|
|
||||||
doesFileExist :: Path Abs -> IO Bool
|
|
||||||
doesFileExist fp =
|
|
||||||
handleIOError (\_ -> return False) $ do
|
|
||||||
fp' <- fmap P.fromAbs $ P.canonicalizePath fp
|
|
||||||
fs <- PF.getFileStatus fp'
|
|
||||||
return $ not . PF.isDirectory $ fs
|
|
||||||
|
|
||||||
|
|
||||||
-- |Checks if the given file exists and is a directory. This follows
|
|
||||||
-- symlinks, but will return False if the symlink is broken.
|
|
||||||
doesDirectoryExist :: Path Abs -> IO Bool
|
|
||||||
doesDirectoryExist fp =
|
|
||||||
handleIOError (\_ -> return False) $ do
|
|
||||||
fp' <- fmap P.fromAbs $ P.canonicalizePath fp
|
|
||||||
fs <- PF.getFileStatus fp'
|
|
||||||
return $ PF.isDirectory fs
|
|
||||||
|
|
||||||
|
|
||||||
-- |Checks whether the directory at the given path exists and can be
|
|
||||||
-- opened. This invokes `openDirStream`.
|
|
||||||
canOpenDirectory :: Path Abs -> IO Bool
|
|
||||||
canOpenDirectory fp =
|
|
||||||
handleIOError (\_ -> return False) $ do
|
|
||||||
bracket (PFD.openDirStream . P.fromAbs $ fp)
|
|
||||||
PFD.closeDirStream
|
|
||||||
(\_ -> return ())
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
-- |Throws a `Can'tOpenDirectory` FmIOException if the directory at the given
|
|
||||||
-- path cannot be opened.
|
|
||||||
throwCantOpenDirectory :: Path Abs -> IO ()
|
|
||||||
throwCantOpenDirectory fp =
|
|
||||||
unlessM (canOpenDirectory fp)
|
|
||||||
(throw . Can'tOpenDirectory . P.fromAbs $ fp)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------------------
|
|
||||||
--[ Error handling functions ]--
|
|
||||||
--------------------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Carries out an action, then checks if there is an IOException and
|
|
||||||
-- a specific errno. If so, then it carries out another action, otherwise
|
|
||||||
-- it rethrows the error.
|
|
||||||
catchErrno :: [Errno] -- ^ errno to catch
|
|
||||||
-> IO a -- ^ action to try, which can raise an IOException
|
|
||||||
-> IO a -- ^ action to carry out in case of an IOException and
|
|
||||||
-- if errno matches
|
|
||||||
-> IO a
|
|
||||||
catchErrno en a1 a2 =
|
|
||||||
catchIOError a1 $ \e -> do
|
|
||||||
errno <- getErrno
|
|
||||||
if errno `elem` en
|
|
||||||
then a2
|
|
||||||
else ioError e
|
|
||||||
|
|
||||||
|
|
||||||
-- |Execute the given action and retrow IO exceptions as a new Exception
|
|
||||||
-- that have the given errno. If errno does not match the exception is rethrown
|
|
||||||
-- as is.
|
|
||||||
rethrowErrnoAs :: Exception e
|
|
||||||
=> [Errno] -- ^ errno to catch
|
|
||||||
-> e -- ^ rethrow as if errno matches
|
|
||||||
-> IO a -- ^ action to try
|
|
||||||
-> IO a
|
|
||||||
rethrowErrnoAs en fmex action = catchErrno en action (throw fmex)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- |Like `catchIOError`, with arguments swapped.
|
|
||||||
handleIOError :: (IOError -> IO a) -> IO a -> IO a
|
|
||||||
handleIOError = flip catchIOError
|
|
||||||
|
|
||||||
@@ -1,611 +0,0 @@
|
|||||||
{--
|
|
||||||
HSFM, a filemanager written in Haskell.
|
|
||||||
Copyright (C) 2016 Julian Ospald
|
|
||||||
|
|
||||||
This program is free software; you can redistribute it and/or
|
|
||||||
modify it under the terms of the GNU General Public License
|
|
||||||
version 2 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 General Public License for more details.
|
|
||||||
|
|
||||||
You should have received a copy of the GNU General Public License
|
|
||||||
along with this program; if not, write to the Free Software
|
|
||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
||||||
--}
|
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# LANGUAGE PackageImports #-}
|
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
||||||
|
|
||||||
-- |This module provides all the atomic IO related file operations like
|
|
||||||
-- copy, delete, move and so on. It operates primarily on `AnchoredFile`, which
|
|
||||||
-- is guaranteed to be well-formed.
|
|
||||||
--
|
|
||||||
-- It would be nicer to pass states around, but the filesystem state changes
|
|
||||||
-- too quickly and cannot be relied upon. Lazy implementations of filesystem
|
|
||||||
-- trees have been tried as well, but they can introduce subtle bugs.
|
|
||||||
module HSFM.FileSystem.FileOperations where
|
|
||||||
|
|
||||||
|
|
||||||
import Control.Exception
|
|
||||||
(
|
|
||||||
bracket
|
|
||||||
, throw
|
|
||||||
)
|
|
||||||
import Control.Monad
|
|
||||||
(
|
|
||||||
unless
|
|
||||||
, void
|
|
||||||
, when
|
|
||||||
)
|
|
||||||
import Data.ByteString
|
|
||||||
(
|
|
||||||
ByteString
|
|
||||||
)
|
|
||||||
import Data.Foldable
|
|
||||||
(
|
|
||||||
for_
|
|
||||||
)
|
|
||||||
import Data.Word
|
|
||||||
(
|
|
||||||
Word8
|
|
||||||
)
|
|
||||||
import Foreign.C.Error
|
|
||||||
(
|
|
||||||
eXDEV
|
|
||||||
, eINVAL
|
|
||||||
, eNOSYS
|
|
||||||
)
|
|
||||||
import Foreign.C.Types
|
|
||||||
(
|
|
||||||
CSize
|
|
||||||
)
|
|
||||||
import Foreign.Marshal.Alloc
|
|
||||||
(
|
|
||||||
allocaBytes
|
|
||||||
)
|
|
||||||
import Foreign.Ptr
|
|
||||||
(
|
|
||||||
Ptr
|
|
||||||
)
|
|
||||||
import HPath
|
|
||||||
(
|
|
||||||
Path
|
|
||||||
, Abs
|
|
||||||
, Fn
|
|
||||||
)
|
|
||||||
import qualified HPath as P
|
|
||||||
import HSFM.FileSystem.Errors
|
|
||||||
import HSFM.FileSystem.FileType
|
|
||||||
import HSFM.Utils.IO
|
|
||||||
import Prelude hiding (readFile)
|
|
||||||
import System.Posix.Directory.ByteString
|
|
||||||
(
|
|
||||||
createDirectory
|
|
||||||
, removeDirectory
|
|
||||||
)
|
|
||||||
import System.Posix.Files.ByteString
|
|
||||||
(
|
|
||||||
createSymbolicLink
|
|
||||||
, fileMode
|
|
||||||
, getFdStatus
|
|
||||||
, groupExecuteMode
|
|
||||||
, groupReadMode
|
|
||||||
, groupWriteMode
|
|
||||||
, otherExecuteMode
|
|
||||||
, otherReadMode
|
|
||||||
, otherWriteMode
|
|
||||||
, ownerModes
|
|
||||||
, ownerReadMode
|
|
||||||
, ownerWriteMode
|
|
||||||
, readSymbolicLink
|
|
||||||
, removeLink
|
|
||||||
, rename
|
|
||||||
, unionFileModes
|
|
||||||
)
|
|
||||||
import qualified System.Posix.Files.ByteString as PF
|
|
||||||
import qualified "unix" System.Posix.IO.ByteString as SPI
|
|
||||||
import qualified "unix-bytestring" System.Posix.IO.ByteString as SPB
|
|
||||||
import System.Posix.IO.Sendfile.ByteString
|
|
||||||
(
|
|
||||||
sendfileFd
|
|
||||||
, FileRange(EntireFile)
|
|
||||||
)
|
|
||||||
import qualified System.Posix.Process.ByteString as SPP
|
|
||||||
import System.Posix.Types
|
|
||||||
(
|
|
||||||
FileMode
|
|
||||||
, ProcessID
|
|
||||||
, Fd
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- TODO: file operations should be threaded and not block the UI
|
|
||||||
-- TODO: make sure we do the right thing for BlockDev, CharDev etc...
|
|
||||||
-- most operations are not implemented for these
|
|
||||||
|
|
||||||
|
|
||||||
-- |Data type describing an actual file operation that can be
|
|
||||||
-- carried out via `doFile`. Useful to build up a list of operations
|
|
||||||
-- or delay operations.
|
|
||||||
data FileOperation = FCopy Copy
|
|
||||||
| FMove Move
|
|
||||||
| FDelete [Path Abs]
|
|
||||||
| FOpen (Path Abs)
|
|
||||||
| FExecute (Path Abs) [ByteString]
|
|
||||||
| None
|
|
||||||
|
|
||||||
|
|
||||||
-- |Data type describing partial or complete file copy operation.
|
|
||||||
-- CC stands for a complete operation and can be used for `runFileOp`.
|
|
||||||
data Copy = CP1 [Path Abs]
|
|
||||||
| CP2 [Path Abs]
|
|
||||||
(Path Abs)
|
|
||||||
| CC [Path Abs]
|
|
||||||
(Path Abs)
|
|
||||||
CopyMode
|
|
||||||
|
|
||||||
|
|
||||||
-- |Data type describing partial or complete file move operation.
|
|
||||||
-- MC stands for a complete operation and can be used for `runFileOp`.
|
|
||||||
data Move = MP1 [Path Abs]
|
|
||||||
| MC [Path Abs]
|
|
||||||
(Path Abs)
|
|
||||||
CopyMode
|
|
||||||
|
|
||||||
|
|
||||||
-- |Copy modes.
|
|
||||||
data CopyMode = Strict -- ^ fail if the target already exists
|
|
||||||
| Merge -- ^ overwrite files if necessary, for files, this
|
|
||||||
-- is the same as Replace
|
|
||||||
| Replace -- ^ remove targets before copying, this is
|
|
||||||
-- only useful if the target is a directorty
|
|
||||||
| Rename (Path Fn)
|
|
||||||
|
|
||||||
|
|
||||||
-- |Run a given FileOperation. If the FileOperation is partial, it will
|
|
||||||
-- be returned. Returns `Nothing` on success.
|
|
||||||
--
|
|
||||||
-- Since file operations can be delayed, this is `Path Abs` based, not
|
|
||||||
-- `File` based. This makes sure we don't have stale
|
|
||||||
-- file information.
|
|
||||||
runFileOp :: FileOperation -> IO (Maybe FileOperation)
|
|
||||||
runFileOp fo' =
|
|
||||||
case fo' of
|
|
||||||
(FCopy (CC froms to cm)) -> do
|
|
||||||
froms' <- mapM toAfile froms
|
|
||||||
to' <- toAfile to
|
|
||||||
when (anyFailed froms')
|
|
||||||
(throw . CopyFailed $ "File in copy buffer does not exist anymore!")
|
|
||||||
mapM_ (\x -> easyCopy cm x to') froms'
|
|
||||||
>> return Nothing
|
|
||||||
(FCopy fo) -> return $ Just $ FCopy fo
|
|
||||||
(FMove (MC froms to cm)) -> do
|
|
||||||
froms' <- mapM toAfile froms
|
|
||||||
to' <- toAfile to
|
|
||||||
when (anyFailed froms')
|
|
||||||
(throw . MoveFailed $ "File in move buffer does not exist anymore!")
|
|
||||||
mapM_ (\x -> easyMove cm x to') froms'
|
|
||||||
>> return Nothing
|
|
||||||
(FMove fo) -> return $ Just $ FMove fo
|
|
||||||
(FDelete fps) -> do
|
|
||||||
fps' <- mapM toAfile fps
|
|
||||||
mapM_ easyDelete fps' >> return Nothing
|
|
||||||
(FOpen fp) ->
|
|
||||||
toAfile fp >>= openFile >> return Nothing
|
|
||||||
(FExecute fp args) ->
|
|
||||||
toAfile fp >>= flip executeFile args >> return Nothing
|
|
||||||
_ -> return Nothing
|
|
||||||
where
|
|
||||||
toAfile = readFile (\_ -> return undefined)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------
|
|
||||||
--[ File Copying ]--
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Copies a directory to the given destination with the specified
|
|
||||||
-- `DirCopyMode`. Excludes symlinks.
|
|
||||||
copyDir :: CopyMode
|
|
||||||
-> File a -- ^ source dir
|
|
||||||
-> File a -- ^ destination dir
|
|
||||||
-> Path Fn -- ^ destination dir name
|
|
||||||
-> IO ()
|
|
||||||
copyDir (Rename fn)
|
|
||||||
from@Dir{}
|
|
||||||
to@Dir{}
|
|
||||||
_
|
|
||||||
= copyDir Strict from to fn
|
|
||||||
-- this branch must never get `Rename` as CopyMode
|
|
||||||
copyDir cm from@Dir{ path = fromp }
|
|
||||||
to@Dir{ path = top }
|
|
||||||
fn
|
|
||||||
= do
|
|
||||||
let destdirp = top P.</> fn
|
|
||||||
-- for performance, sanity checks are only done for the top dir
|
|
||||||
throwDestinationInSource fromp destdirp
|
|
||||||
throwSameFile fromp destdirp
|
|
||||||
throwCantOpenDirectory fromp
|
|
||||||
throwCantOpenDirectory top
|
|
||||||
go cm from to fn
|
|
||||||
where
|
|
||||||
go :: CopyMode -> File a -> File a -> Path Fn -> IO ()
|
|
||||||
go cm' Dir{ path = fromp' }
|
|
||||||
Dir{ path = top' }
|
|
||||||
fn' = do
|
|
||||||
fmode' <- PF.fileMode <$> PF.getSymbolicLinkStatus
|
|
||||||
(P.fromAbs fromp')
|
|
||||||
createDestdir (top' P.</> fn') fmode'
|
|
||||||
destdir <- readFile (\_ -> return undefined)
|
|
||||||
(top' P.</> fn')
|
|
||||||
contents <- readDirectoryContents
|
|
||||||
(\_ -> return undefined) fromp'
|
|
||||||
|
|
||||||
for_ contents $ \f ->
|
|
||||||
case f of
|
|
||||||
SymLink{ path = fp' } -> recreateSymlink cm' f destdir
|
|
||||||
=<< (P.basename fp')
|
|
||||||
Dir{ path = fp' } -> go cm' f destdir
|
|
||||||
=<< (P.basename fp')
|
|
||||||
RegFile{ path = fp' } -> unsafeCopyFile Replace f destdir
|
|
||||||
=<< (P.basename fp')
|
|
||||||
_ -> return ()
|
|
||||||
where
|
|
||||||
createDestdir destdir fmode' =
|
|
||||||
let destdir' = P.toFilePath destdir
|
|
||||||
in case cm' of
|
|
||||||
Merge ->
|
|
||||||
unlessM (doesDirectoryExist destdir)
|
|
||||||
(createDirectory destdir' fmode')
|
|
||||||
Strict -> do
|
|
||||||
throwDirDoesExist destdir
|
|
||||||
createDirectory destdir' fmode'
|
|
||||||
Replace -> do
|
|
||||||
whenM (doesDirectoryExist destdir)
|
|
||||||
(deleteDirRecursive =<<
|
|
||||||
readFile
|
|
||||||
(\_ -> return undefined) destdir)
|
|
||||||
createDirectory destdir' fmode'
|
|
||||||
_ -> throw $ InvalidOperation "Internal error, wrong CopyMode!"
|
|
||||||
go _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
copyDir _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Recreate a symlink.
|
|
||||||
recreateSymlink :: CopyMode
|
|
||||||
-> File a -- ^ the old symlink file
|
|
||||||
-> File a -- ^ destination dir of the
|
|
||||||
-- new symlink file
|
|
||||||
-> Path Fn -- ^ destination file name
|
|
||||||
-> IO ()
|
|
||||||
recreateSymlink (Rename pn) symf@SymLink{} symdest@Dir{} _
|
|
||||||
= recreateSymlink Strict symf symdest pn
|
|
||||||
recreateSymlink cm SymLink{ path = sfp } Dir{ path = sdp } fn
|
|
||||||
= do
|
|
||||||
throwCantOpenDirectory sdp
|
|
||||||
sympoint <- readSymbolicLink (P.fromAbs sfp)
|
|
||||||
let symname = sdp P.</> fn
|
|
||||||
case cm of
|
|
||||||
Merge -> delOld symname
|
|
||||||
Replace -> delOld symname
|
|
||||||
_ -> return ()
|
|
||||||
createSymbolicLink sympoint (P.fromAbs symname)
|
|
||||||
where
|
|
||||||
delOld symname = do
|
|
||||||
f <- readFile (\_ -> return undefined) symname
|
|
||||||
unless (failed f)
|
|
||||||
(easyDelete f)
|
|
||||||
recreateSymlink _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Copies the given regular file to the given dir with the given filename.
|
|
||||||
-- Excludes symlinks.
|
|
||||||
copyFile :: CopyMode
|
|
||||||
-> File a -- ^ source file
|
|
||||||
-> File a -- ^ destination dir
|
|
||||||
-> Path Fn -- ^ destination file name
|
|
||||||
-> IO ()
|
|
||||||
copyFile (Rename pn) from@RegFile{} to@Dir{} _
|
|
||||||
= copyFile Strict from to pn
|
|
||||||
copyFile cm from@RegFile{ path = fromp }
|
|
||||||
tod@Dir{ path = todp } fn
|
|
||||||
= do
|
|
||||||
throwCantOpenDirectory todp
|
|
||||||
throwCantOpenDirectory . P.dirname $ fromp
|
|
||||||
throwSameFile fromp (todp P.</> fn)
|
|
||||||
unsafeCopyFile cm from tod fn
|
|
||||||
copyFile _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Unsafe version of `copyFile` without initial sanity checks. This
|
|
||||||
-- holds the actual copy logic though and is called by `copyFile` in the end.
|
|
||||||
-- It's also used for cases where we don't need/want sanity checks
|
|
||||||
-- and need the extra bit of performance.
|
|
||||||
unsafeCopyFile :: CopyMode
|
|
||||||
-> File a -- ^ source file
|
|
||||||
-> File a -- ^ destination dir
|
|
||||||
-> Path Fn -- ^ destination file name
|
|
||||||
-> IO ()
|
|
||||||
unsafeCopyFile (Rename pn) from@RegFile{} to@Dir{} _
|
|
||||||
= copyFile Strict from to pn
|
|
||||||
unsafeCopyFile cm RegFile{ path = fromp }
|
|
||||||
Dir{ path = todp } fn
|
|
||||||
= do
|
|
||||||
let to = todp P.</> fn
|
|
||||||
case cm of
|
|
||||||
Strict -> throwFileDoesExist to
|
|
||||||
_ -> return ()
|
|
||||||
|
|
||||||
-- from sendfile(2) manpage:
|
|
||||||
-- Applications may wish to fall back to read(2)/write(2) in the case
|
|
||||||
-- where sendfile() fails with EINVAL or ENOSYS.
|
|
||||||
P.withAbsPath to $ \to' -> P.withAbsPath fromp $ \from' ->
|
|
||||||
catchErrno [eINVAL, eNOSYS]
|
|
||||||
(sendFileCopy from' to')
|
|
||||||
(void $ fallbackCopy from' to')
|
|
||||||
where
|
|
||||||
-- this is low-level stuff utilizing sendfile(2) for speed
|
|
||||||
sendFileCopy source dest =
|
|
||||||
-- NOTE: we are not blocking IO here, O_NONBLOCK is false
|
|
||||||
-- for `defaultFileFlags`
|
|
||||||
bracket (SPI.openFd source SPI.ReadOnly Nothing SPI.defaultFileFlags)
|
|
||||||
SPI.closeFd
|
|
||||||
$ \sfd -> do
|
|
||||||
fileM <- System.Posix.Files.ByteString.fileMode
|
|
||||||
<$> getFdStatus sfd
|
|
||||||
bracket (SPI.openFd dest SPI.WriteOnly (Just fileM)
|
|
||||||
SPI.defaultFileFlags)
|
|
||||||
SPI.closeFd
|
|
||||||
$ \dfd -> sendfileFd dfd sfd EntireFile
|
|
||||||
-- low-level copy operation utilizing read(2)/write(2)
|
|
||||||
-- in case `sendFileCopy` fails/is unsupported
|
|
||||||
fallbackCopy source dest =
|
|
||||||
bracket (SPI.openFd source SPI.ReadOnly Nothing SPI.defaultFileFlags)
|
|
||||||
SPI.closeFd
|
|
||||||
$ \sfd -> do
|
|
||||||
fileM <- System.Posix.Files.ByteString.fileMode
|
|
||||||
<$> getFdStatus sfd
|
|
||||||
bracket (SPI.openFd dest SPI.WriteOnly (Just fileM)
|
|
||||||
SPI.defaultFileFlags)
|
|
||||||
SPI.closeFd
|
|
||||||
$ \dfd -> allocaBytes (fromIntegral bufSize) $ \buf ->
|
|
||||||
write' sfd dfd buf 0
|
|
||||||
where
|
|
||||||
bufSize :: CSize
|
|
||||||
bufSize = 8192
|
|
||||||
write' :: Fd -> Fd -> Ptr Word8 -> Int -> IO Int
|
|
||||||
write' sfd dfd buf totalsize = do
|
|
||||||
size <- SPB.fdReadBuf sfd buf bufSize
|
|
||||||
if size == 0
|
|
||||||
then return $ fromIntegral totalsize
|
|
||||||
else do rsize <- SPB.fdWriteBuf dfd buf size
|
|
||||||
when (rsize /= size) (throw . CopyFailed $ "wrong size!")
|
|
||||||
write' sfd dfd buf (totalsize + fromIntegral size)
|
|
||||||
unsafeCopyFile _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Copies a regular file, directory or symlink. In case of a symlink,
|
|
||||||
-- it is just recreated, even if it points to a directory.
|
|
||||||
easyCopy :: CopyMode
|
|
||||||
-> File a
|
|
||||||
-> File a
|
|
||||||
-> IO ()
|
|
||||||
easyCopy cm from@SymLink{}
|
|
||||||
to@Dir{}
|
|
||||||
= recreateSymlink cm from to =<< (P.basename . path $ from)
|
|
||||||
easyCopy cm from@RegFile{}
|
|
||||||
to@Dir{}
|
|
||||||
= copyFile cm from to =<< (P.basename . path $ from)
|
|
||||||
easyCopy cm from@Dir{}
|
|
||||||
to@Dir{}
|
|
||||||
= copyDir cm from to =<< (P.basename . path $ from)
|
|
||||||
easyCopy _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---------------------
|
|
||||||
--[ File Deletion ]--
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Deletes a symlink, which can either point to a file or directory.
|
|
||||||
deleteSymlink :: File a -> IO ()
|
|
||||||
deleteSymlink SymLink{ path = fp }
|
|
||||||
= P.withAbsPath fp removeLink
|
|
||||||
deleteSymlink _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Deletes the given regular file, never symlinks.
|
|
||||||
deleteFile :: File a -> IO ()
|
|
||||||
deleteFile RegFile{ path = fp }
|
|
||||||
= P.withAbsPath fp removeLink
|
|
||||||
deleteFile _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Deletes the given directory, never symlinks.
|
|
||||||
deleteDir :: File a -> IO ()
|
|
||||||
deleteDir Dir{ path = fp }
|
|
||||||
= P.withAbsPath fp removeDirectory
|
|
||||||
deleteDir _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Deletes the given directory recursively.
|
|
||||||
deleteDirRecursive :: File a -> IO ()
|
|
||||||
deleteDirRecursive f'@Dir{ path = fp' } = do
|
|
||||||
throwCantOpenDirectory fp'
|
|
||||||
go f'
|
|
||||||
where
|
|
||||||
go :: File a -> IO ()
|
|
||||||
go Dir{ path = fp } = do
|
|
||||||
files <- readDirectoryContents
|
|
||||||
(\_ -> return undefined) fp
|
|
||||||
for_ files $ \file ->
|
|
||||||
case file of
|
|
||||||
SymLink{} -> deleteSymlink file
|
|
||||||
Dir{} -> go file
|
|
||||||
RegFile{ path = rfp }
|
|
||||||
-> P.withAbsPath rfp removeLink
|
|
||||||
_ -> throw $ FileDoesExist
|
|
||||||
(P.toFilePath . path $ file)
|
|
||||||
removeDirectory . P.toFilePath $ fp
|
|
||||||
go _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
deleteDirRecursive _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Deletes a file, directory or symlink, whatever it may be.
|
|
||||||
-- In case of directory, performs recursive deletion. In case of
|
|
||||||
-- a symlink, the symlink file is deleted.
|
|
||||||
easyDelete :: File a -> IO ()
|
|
||||||
easyDelete f@SymLink{} = deleteSymlink f
|
|
||||||
easyDelete f@RegFile{}
|
|
||||||
= deleteFile f
|
|
||||||
easyDelete f@Dir{}
|
|
||||||
= deleteDirRecursive f
|
|
||||||
easyDelete _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--------------------
|
|
||||||
--[ File Opening ]--
|
|
||||||
--------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Opens a file appropriately by invoking xdg-open. The file type
|
|
||||||
-- is not checked.
|
|
||||||
openFile :: File a
|
|
||||||
-> IO ProcessID
|
|
||||||
openFile f =
|
|
||||||
P.withAbsPath (path f) $ \fp ->
|
|
||||||
SPP.forkProcess $ SPP.executeFile "xdg-open" True [fp] Nothing
|
|
||||||
|
|
||||||
|
|
||||||
-- |Executes a program with the given arguments.
|
|
||||||
executeFile :: File a -- ^ program
|
|
||||||
-> [ByteString] -- ^ arguments
|
|
||||||
-> IO ProcessID
|
|
||||||
executeFile RegFile{ path = fp } args
|
|
||||||
= P.withAbsPath fp $ \fpb ->
|
|
||||||
SPP.forkProcess
|
|
||||||
$ SPP.executeFile fpb True args Nothing
|
|
||||||
executeFile SymLink{ path = fp, sdest = RegFile{} } args
|
|
||||||
= P.withAbsPath fp $ \fpb ->
|
|
||||||
SPP.forkProcess
|
|
||||||
$ SPP.executeFile fpb True args Nothing
|
|
||||||
executeFile _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---------------------
|
|
||||||
--[ File Creation ]--
|
|
||||||
---------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Create an empty regular file at the given directory with the given filename.
|
|
||||||
createFile :: File FileInfo -> Path Fn -> IO ()
|
|
||||||
createFile (DirOrSym td) fn = do
|
|
||||||
let fullp = path td P.</> fn
|
|
||||||
throwFileDoesExist fullp
|
|
||||||
fd <- SPI.createFile (P.fromAbs fullp) newFilePerms
|
|
||||||
SPI.closeFd fd
|
|
||||||
createFile _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Create an empty directory at the given directory with the given filename.
|
|
||||||
createDir :: File FileInfo -> Path Fn -> IO ()
|
|
||||||
createDir (DirOrSym td) fn = do
|
|
||||||
let fullp = path td P.</> fn
|
|
||||||
throwDirDoesExist fullp
|
|
||||||
createDirectory (P.fromAbs fullp) newFilePerms
|
|
||||||
createDir _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
----------------------------
|
|
||||||
--[ File Renaming/Moving ]--
|
|
||||||
----------------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Rename a given file with the provided filename.
|
|
||||||
renameFile :: File a -> Path Fn -> IO ()
|
|
||||||
renameFile af fn = do
|
|
||||||
let fromf = path af
|
|
||||||
tof = (P.dirname . path $ af) P.</> fn
|
|
||||||
throwFileDoesExist tof
|
|
||||||
throwSameFile fromf tof
|
|
||||||
rename (P.fromAbs fromf) (P.fromAbs tof)
|
|
||||||
|
|
||||||
|
|
||||||
-- |Move a given file to the given target directory.
|
|
||||||
moveFile :: CopyMode
|
|
||||||
-> File a -- ^ file to move
|
|
||||||
-> File a -- ^ base target directory
|
|
||||||
-> Path Fn -- ^ target file name
|
|
||||||
-> IO ()
|
|
||||||
moveFile (Rename pn) from to@Dir{} _ =
|
|
||||||
moveFile Strict from to pn
|
|
||||||
moveFile cm from to@Dir{} fn = do
|
|
||||||
let from' = path from
|
|
||||||
froms' = P.fromAbs from'
|
|
||||||
to' = path to P.</> fn
|
|
||||||
tos' = P.fromAbs to'
|
|
||||||
case cm of
|
|
||||||
Strict -> throwFileDoesExist to'
|
|
||||||
Merge -> delOld to'
|
|
||||||
Replace -> delOld to'
|
|
||||||
Rename _ -> throw $ InvalidOperation "Internal error! Wrong CopyMode!"
|
|
||||||
throwSameFile from' to'
|
|
||||||
catchErrno [eXDEV] (rename froms' tos') $ do
|
|
||||||
easyCopy Strict from to
|
|
||||||
easyDelete from
|
|
||||||
where
|
|
||||||
delOld fp = do
|
|
||||||
to' <- readFile (\_ -> return undefined) fp
|
|
||||||
unless (failed to') (easyDelete to')
|
|
||||||
moveFile _ _ _ _ = throw $ InvalidOperation "wrong input type"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Like `moveFile` except it uses the filename of the source as target.
|
|
||||||
easyMove :: CopyMode
|
|
||||||
-> File a -- ^ file to move
|
|
||||||
-> File a -- ^ base target directory
|
|
||||||
-> IO ()
|
|
||||||
easyMove cm from to = moveFile cm from to =<< (P.basename . path $ from)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
|
||||||
--[ File Permissions]--
|
|
||||||
-----------------------
|
|
||||||
|
|
||||||
|
|
||||||
-- |Default permissions for a new file.
|
|
||||||
newFilePerms :: FileMode
|
|
||||||
newFilePerms
|
|
||||||
= ownerWriteMode
|
|
||||||
`unionFileModes` ownerReadMode
|
|
||||||
`unionFileModes` groupWriteMode
|
|
||||||
`unionFileModes` groupReadMode
|
|
||||||
`unionFileModes` otherWriteMode
|
|
||||||
`unionFileModes` otherReadMode
|
|
||||||
|
|
||||||
|
|
||||||
-- |Default permissions for a new directory.
|
|
||||||
newDirPerms :: FileMode
|
|
||||||
newDirPerms
|
|
||||||
= ownerModes
|
|
||||||
`unionFileModes` groupExecuteMode
|
|
||||||
`unionFileModes` groupReadMode
|
|
||||||
`unionFileModes` otherExecuteMode
|
|
||||||
`unionFileModes` otherReadMode
|
|
||||||
|
|
||||||
@@ -18,20 +18,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||||||
|
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
-- |This module provides data types for representing directories/files
|
|
||||||
-- and related operations on it, mostly internal stuff.
|
-- |This module provides a data type for representing directories/files
|
||||||
|
-- in a well-typed and convenient way. This is useful to gather and
|
||||||
|
-- save information about a file, so the information can be easily
|
||||||
|
-- processed in e.g. a GUI.
|
||||||
--
|
--
|
||||||
-- It doesn't allow to represent the whole filesystem, since that's only
|
-- However, it's not meant to be used to interact with low-level
|
||||||
-- possible through IO laziness, which introduces too much internal state.
|
-- functions that copy files etc, since there's no guarantee that
|
||||||
|
-- the in-memory representation of the type still matches what is
|
||||||
|
-- happening on filesystem level.
|
||||||
|
--
|
||||||
|
-- If you interact with low-level libraries, you must not pattern
|
||||||
|
-- match on the `File a` type. Instead, you should only use the saved
|
||||||
|
-- `path` and make no assumptions about the file the path might or
|
||||||
|
-- might not point to.
|
||||||
module HSFM.FileSystem.FileType where
|
module HSFM.FileSystem.FileType where
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
import Data.ByteString(ByteString)
|
import Data.ByteString(ByteString)
|
||||||
import Data.Default
|
import Data.ByteString.UTF8
|
||||||
import Data.Maybe
|
|
||||||
(
|
(
|
||||||
catMaybes
|
toString
|
||||||
)
|
)
|
||||||
import Data.Time.Clock.POSIX
|
import Data.Time.Clock.POSIX
|
||||||
(
|
(
|
||||||
@@ -39,33 +48,22 @@ import Data.Time.Clock.POSIX
|
|||||||
, posixSecondsToUTCTime
|
, posixSecondsToUTCTime
|
||||||
)
|
)
|
||||||
import Data.Time()
|
import Data.Time()
|
||||||
import Foreign.C.Error
|
|
||||||
(
|
|
||||||
eACCES
|
|
||||||
)
|
|
||||||
import HPath
|
import HPath
|
||||||
(
|
(
|
||||||
Abs
|
Abs
|
||||||
, Path
|
, Path
|
||||||
, Fn
|
|
||||||
)
|
)
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
import HSFM.FileSystem.Errors
|
import HPath.IO hiding (FileType(..))
|
||||||
import HSFM.Utils.MyPrelude
|
import HPath.IO.Errors
|
||||||
import Prelude hiding(readFile)
|
import Prelude hiding(readFile)
|
||||||
import System.IO.Error
|
|
||||||
(
|
|
||||||
ioeGetErrorType
|
|
||||||
, isDoesNotExistErrorType
|
|
||||||
)
|
|
||||||
import System.Posix.FilePath
|
import System.Posix.FilePath
|
||||||
(
|
(
|
||||||
(</>)
|
(</>)
|
||||||
)
|
)
|
||||||
import System.Posix.Directory.Traversals
|
import System.Posix.Directory.Traversals
|
||||||
(
|
(
|
||||||
getDirectoryContents
|
realpath
|
||||||
, realpath
|
|
||||||
)
|
)
|
||||||
import qualified System.Posix.Files.ByteString as PF
|
import qualified System.Posix.Files.ByteString as PF
|
||||||
import System.Posix.Types
|
import System.Posix.Types
|
||||||
@@ -93,62 +91,57 @@ import System.Posix.Types
|
|||||||
-- |The String in the path field is always a full path.
|
-- |The String in the path field is always a full path.
|
||||||
-- The free type variable is used in the File/Dir constructor and can hold
|
-- The free type variable is used in the File/Dir constructor and can hold
|
||||||
-- Handles, Strings representing a file's contents or anything else you can
|
-- Handles, Strings representing a file's contents or anything else you can
|
||||||
-- think of. We catch any IO errors in the Failed constructor. an Exception
|
-- think of.
|
||||||
-- can be converted to a String with 'show'.
|
|
||||||
data File a =
|
data File a =
|
||||||
Failed {
|
Dir {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, err :: IOError
|
|
||||||
}
|
|
||||||
| Dir {
|
|
||||||
path :: Path Abs
|
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
}
|
}
|
||||||
| RegFile {
|
| RegFile {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
}
|
}
|
||||||
| SymLink {
|
| SymLink {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
, sdest :: File a -- ^ symlink madness,
|
, sdest :: Maybe (File a) -- ^ symlink madness,
|
||||||
-- we need to know where it points to
|
-- we need to know where it points to
|
||||||
, rawdest :: ByteString
|
, rawdest :: !ByteString
|
||||||
}
|
}
|
||||||
| BlockDev {
|
| BlockDev {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
}
|
}
|
||||||
| CharDev {
|
| CharDev {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
}
|
}
|
||||||
| NamedPipe {
|
| NamedPipe {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
}
|
}
|
||||||
| Socket {
|
| Socket {
|
||||||
path :: Path Abs
|
path :: !(Path Abs)
|
||||||
, fvar :: a
|
, fvar :: a
|
||||||
} deriving (Show, Eq)
|
} deriving (Show, Eq)
|
||||||
|
|
||||||
|
|
||||||
-- |Low-level file information.
|
-- |Low-level file information.
|
||||||
data FileInfo = FileInfo {
|
data FileInfo = FileInfo {
|
||||||
deviceID :: DeviceID
|
deviceID :: !DeviceID
|
||||||
, fileID :: FileID
|
, fileID :: !FileID
|
||||||
, fileMode :: FileMode
|
, fileMode :: !FileMode
|
||||||
, linkCount :: LinkCount
|
, linkCount :: !LinkCount
|
||||||
, fileOwner :: UserID
|
, fileOwner :: !UserID
|
||||||
, fileGroup :: GroupID
|
, fileGroup :: !GroupID
|
||||||
, specialDeviceID :: DeviceID
|
, specialDeviceID :: !DeviceID
|
||||||
, fileSize :: FileOffset
|
, fileSize :: !FileOffset
|
||||||
, accessTime :: EpochTime
|
, accessTime :: !EpochTime
|
||||||
, modificationTime :: EpochTime
|
, modificationTime :: !EpochTime
|
||||||
, statusChangeTime :: EpochTime
|
, statusChangeTime :: !EpochTime
|
||||||
, accessTimeHiRes :: POSIXTime
|
, accessTimeHiRes :: !POSIXTime
|
||||||
, modificationTimeHiRes :: POSIXTime
|
, modificationTimeHiRes :: !POSIXTime
|
||||||
, statusChangeTimeHiRes :: POSIXTime
|
, statusChangeTimeHiRes :: !POSIXTime
|
||||||
} deriving (Show, Eq, Ord)
|
} deriving (Show, Eq, Ord)
|
||||||
|
|
||||||
|
|
||||||
@@ -183,28 +176,31 @@ fileLike f = (False, f)
|
|||||||
|
|
||||||
|
|
||||||
sdir :: File FileInfo -> (Bool, File FileInfo)
|
sdir :: File FileInfo -> (Bool, File FileInfo)
|
||||||
sdir f@SymLink{ sdest = (s@SymLink{} )}
|
sdir f@SymLink{ sdest = (Just s@SymLink{} )}
|
||||||
-- we have to follow a chain of symlinks here, but
|
-- we have to follow a chain of symlinks here, but
|
||||||
-- return only the very first level
|
-- return only the very first level
|
||||||
-- TODO: this is probably obsolete now
|
-- TODO: this is probably obsolete now
|
||||||
= case sdir s of
|
= case sdir s of
|
||||||
(True, _) -> (True, f)
|
(True, _) -> (True, f)
|
||||||
_ -> (False, f)
|
_ -> (False, f)
|
||||||
sdir f@SymLink{ sdest = Dir{} }
|
sdir f@SymLink{ sdest = Just Dir{} }
|
||||||
= (True, f)
|
= (True, f)
|
||||||
sdir f@Dir{} = (True, f)
|
sdir f@Dir{} = (True, f)
|
||||||
sdir f = (False, f)
|
sdir f = (False, f)
|
||||||
|
|
||||||
|
|
||||||
-- |Matches on any non-directory kind of files, excluding symlinks.
|
-- |Matches on any non-directory kind of files, excluding symlinks.
|
||||||
|
pattern FileLike :: File FileInfo -> File FileInfo
|
||||||
pattern FileLike f <- (fileLike -> (True, f))
|
pattern FileLike f <- (fileLike -> (True, f))
|
||||||
|
|
||||||
-- |Matches a list of directories or symlinks pointing to directories.
|
-- |Matches a list of directories or symlinks pointing to directories.
|
||||||
|
pattern DirList :: [File FileInfo] -> [File FileInfo]
|
||||||
pattern DirList fs <- (\fs -> (and . fmap (fst . sdir) $ fs, fs)
|
pattern DirList fs <- (\fs -> (and . fmap (fst . sdir) $ fs, fs)
|
||||||
-> (True, fs))
|
-> (True, fs))
|
||||||
|
|
||||||
-- |Matches a list of any non-directory kind of files or symlinks
|
-- |Matches a list of any non-directory kind of files or symlinks
|
||||||
-- pointing to such.
|
-- pointing to such.
|
||||||
|
pattern FileLikeList :: [File FileInfo] -> [File FileInfo]
|
||||||
pattern FileLikeList fs <- (\fs -> (and
|
pattern FileLikeList fs <- (\fs -> (and
|
||||||
. fmap (fst . sfileLike)
|
. fmap (fst . sfileLike)
|
||||||
$ fs, fs) -> (True, fs))
|
$ fs, fs) -> (True, fs))
|
||||||
@@ -219,31 +215,33 @@ brokenSymlink f = (isBrokenSymlink f, f)
|
|||||||
|
|
||||||
|
|
||||||
fileLikeSym :: File FileInfo -> (Bool, File FileInfo)
|
fileLikeSym :: File FileInfo -> (Bool, File FileInfo)
|
||||||
fileLikeSym f@SymLink{ sdest = s@SymLink{} }
|
fileLikeSym f@SymLink{ sdest = Just s@SymLink{} }
|
||||||
= case fileLikeSym s of
|
= case fileLikeSym s of
|
||||||
(True, _) -> (True, f)
|
(True, _) -> (True, f)
|
||||||
_ -> (False, f)
|
_ -> (False, f)
|
||||||
fileLikeSym f@SymLink{ sdest = RegFile{} } = (True, f)
|
fileLikeSym f@SymLink{ sdest = Just RegFile{} } = (True, f)
|
||||||
fileLikeSym f@SymLink{ sdest = BlockDev{} } = (True, f)
|
fileLikeSym f@SymLink{ sdest = Just BlockDev{} } = (True, f)
|
||||||
fileLikeSym f@SymLink{ sdest = CharDev{} } = (True, f)
|
fileLikeSym f@SymLink{ sdest = Just CharDev{} } = (True, f)
|
||||||
fileLikeSym f@SymLink{ sdest = NamedPipe{} } = (True, f)
|
fileLikeSym f@SymLink{ sdest = Just NamedPipe{} } = (True, f)
|
||||||
fileLikeSym f@SymLink{ sdest = Socket{} } = (True, f)
|
fileLikeSym f@SymLink{ sdest = Just Socket{} } = (True, f)
|
||||||
fileLikeSym f = (False, f)
|
fileLikeSym f = (False, f)
|
||||||
|
|
||||||
|
|
||||||
dirSym :: File FileInfo -> (Bool, File FileInfo)
|
dirSym :: File FileInfo -> (Bool, File FileInfo)
|
||||||
dirSym f@SymLink{ sdest = s@SymLink{} }
|
dirSym f@SymLink{ sdest = Just s@SymLink{} }
|
||||||
= case dirSym s of
|
= case dirSym s of
|
||||||
(True, _) -> (True, f)
|
(True, _) -> (True, f)
|
||||||
_ -> (False, f)
|
_ -> (False, f)
|
||||||
dirSym f@SymLink{ sdest = Dir{} } = (True, f)
|
dirSym f@SymLink{ sdest = Just Dir{} } = (True, f)
|
||||||
dirSym f = (False, f)
|
dirSym f = (False, f)
|
||||||
|
|
||||||
|
|
||||||
-- |Matches on symlinks pointing to file-like files only.
|
-- |Matches on symlinks pointing to file-like files only.
|
||||||
|
pattern FileLikeSym :: File FileInfo -> File FileInfo
|
||||||
pattern FileLikeSym f <- (fileLikeSym -> (True, f))
|
pattern FileLikeSym f <- (fileLikeSym -> (True, f))
|
||||||
|
|
||||||
-- |Matches on broken symbolic links.
|
-- |Matches on broken symbolic links.
|
||||||
|
pattern BrokenSymlink :: File FileInfo -> File FileInfo
|
||||||
pattern BrokenSymlink f <- (brokenSymlink -> (True, f))
|
pattern BrokenSymlink f <- (brokenSymlink -> (True, f))
|
||||||
|
|
||||||
|
|
||||||
@@ -251,9 +249,11 @@ pattern BrokenSymlink f <- (brokenSymlink -> (True, f))
|
|||||||
-- If the symlink is pointing to a symlink pointing to a directory, then
|
-- If the symlink is pointing to a symlink pointing to a directory, then
|
||||||
-- it will return True, but also return the first element in the symlink-
|
-- it will return True, but also return the first element in the symlink-
|
||||||
-- chain, not the last.
|
-- chain, not the last.
|
||||||
|
pattern DirOrSym :: File FileInfo -> File FileInfo
|
||||||
pattern DirOrSym f <- (sdir -> (True, f))
|
pattern DirOrSym f <- (sdir -> (True, f))
|
||||||
|
|
||||||
-- |Matches on symlinks pointing to directories only.
|
-- |Matches on symlinks pointing to directories only.
|
||||||
|
pattern DirSym :: File FileInfo -> File FileInfo
|
||||||
pattern DirSym f <- (dirSym -> (True, f))
|
pattern DirSym f <- (dirSym -> (True, f))
|
||||||
|
|
||||||
-- |Matches on any non-directory kind of files or symlinks pointing to
|
-- |Matches on any non-directory kind of files or symlinks pointing to
|
||||||
@@ -261,6 +261,7 @@ pattern DirSym f <- (dirSym -> (True, f))
|
|||||||
-- If the symlink is pointing to a symlink pointing to such a file, then
|
-- If the symlink is pointing to a symlink pointing to such a file, then
|
||||||
-- it will return True, but also return the first element in the symlink-
|
-- it will return True, but also return the first element in the symlink-
|
||||||
-- chain, not the last.
|
-- chain, not the last.
|
||||||
|
pattern FileLikeOrSym :: File FileInfo -> File FileInfo
|
||||||
pattern FileLikeOrSym f <- (sfileLike -> (True, f))
|
pattern FileLikeOrSym f <- (sfileLike -> (True, f))
|
||||||
|
|
||||||
|
|
||||||
@@ -299,11 +300,10 @@ instance Ord (File FileInfo) where
|
|||||||
|
|
||||||
-- |Reads a file or directory Path into an `AnchoredFile`, filling the free
|
-- |Reads a file or directory Path into an `AnchoredFile`, filling the free
|
||||||
-- variables via the given function.
|
-- variables via the given function.
|
||||||
readFile :: (Path Abs -> IO a)
|
pathToFile :: (Path Abs -> IO a)
|
||||||
-> Path Abs
|
-> Path Abs
|
||||||
-> IO (File a)
|
-> IO (File a)
|
||||||
readFile ff p =
|
pathToFile ff p = do
|
||||||
handleDT p $ do
|
|
||||||
fs <- PF.getSymbolicLinkStatus (P.toFilePath p)
|
fs <- PF.getSymbolicLinkStatus (P.toFilePath p)
|
||||||
fv <- ff p
|
fv <- ff p
|
||||||
constructFile fs fv p
|
constructFile fs fv p
|
||||||
@@ -313,11 +313,12 @@ readFile ff p =
|
|||||||
-- symlink madness, we need to make sure we save the correct
|
-- symlink madness, we need to make sure we save the correct
|
||||||
-- File
|
-- File
|
||||||
x <- PF.readSymbolicLink (P.fromAbs p')
|
x <- PF.readSymbolicLink (P.fromAbs p')
|
||||||
resolvedSyml <- handleDT p' $ do
|
resolvedSyml <- handleIOError (\_ -> return Nothing) $ do
|
||||||
-- watch out, we call </> from 'filepath' here, but it is safe
|
-- watch out, we call </> from 'filepath' here, but it is safe
|
||||||
let sfp = (P.fromAbs . P.dirname $ p') </> x
|
let sfp = (P.fromAbs . P.dirname $ p') </> x
|
||||||
rsfp <- realpath sfp
|
rsfp <- realpath sfp
|
||||||
readFile ff =<< P.parseAbs rsfp
|
f <- pathToFile ff =<< P.parseAbs rsfp
|
||||||
|
return $ Just f
|
||||||
return $ SymLink p' fv resolvedSyml x
|
return $ SymLink p' fv resolvedSyml x
|
||||||
| PF.isDirectory fs = return $ Dir p' fv
|
| PF.isDirectory fs = return $ Dir p' fv
|
||||||
| PF.isRegularFile fs = return $ RegFile p' fv
|
| PF.isRegularFile fs = return $ RegFile p' fv
|
||||||
@@ -325,8 +326,7 @@ readFile ff p =
|
|||||||
| PF.isCharacterDevice fs = return $ CharDev p' fv
|
| PF.isCharacterDevice fs = return $ CharDev p' fv
|
||||||
| PF.isNamedPipe fs = return $ NamedPipe p' fv
|
| PF.isNamedPipe fs = return $ NamedPipe p' fv
|
||||||
| PF.isSocket fs = return $ Socket p' fv
|
| PF.isSocket fs = return $ Socket p' fv
|
||||||
| otherwise = return $ Failed p' (userError
|
| otherwise = ioError $ userError "Unknown filetype!"
|
||||||
"Unknown filetype!")
|
|
||||||
|
|
||||||
|
|
||||||
-- |Get the contents of a given directory and return them as a list
|
-- |Get the contents of a given directory and return them as a list
|
||||||
@@ -336,11 +336,10 @@ readDirectoryContents :: (Path Abs -> IO a) -- ^ fills free a variable
|
|||||||
-> IO [File a]
|
-> IO [File a]
|
||||||
readDirectoryContents ff p = do
|
readDirectoryContents ff p = do
|
||||||
files <- getDirsFiles p
|
files <- getDirsFiles p
|
||||||
fcs <- mapM (readFile ff) files
|
mapM (pathToFile ff) files
|
||||||
return $ removeNonexistent fcs
|
|
||||||
|
|
||||||
|
|
||||||
-- |A variant of `readDirectoryContents` where the third argument
|
-- |A variant of `readDirectoryContents` where the second argument
|
||||||
-- is a `File`. If a non-directory is passed returns an empty list.
|
-- is a `File`. If a non-directory is passed returns an empty list.
|
||||||
getContents :: (Path Abs -> IO a)
|
getContents :: (Path Abs -> IO a)
|
||||||
-> File FileInfo
|
-> File FileInfo
|
||||||
@@ -353,12 +352,12 @@ getContents _ _ = return []
|
|||||||
|
|
||||||
-- |Go up one directory in the filesystem hierarchy.
|
-- |Go up one directory in the filesystem hierarchy.
|
||||||
goUp :: File FileInfo -> IO (File FileInfo)
|
goUp :: File FileInfo -> IO (File FileInfo)
|
||||||
goUp file = readFile getFileInfo (P.dirname . path $ file)
|
goUp file = pathToFile getFileInfo (P.dirname . path $ file)
|
||||||
|
|
||||||
|
|
||||||
-- |Go up one directory in the filesystem hierarchy.
|
-- |Go up one directory in the filesystem hierarchy.
|
||||||
goUp' :: Path Abs -> IO (File FileInfo)
|
goUp' :: Path Abs -> IO (File FileInfo)
|
||||||
goUp' fp = readFile getFileInfo $ P.dirname fp
|
goUp' fp = pathToFile getFileInfo $ P.dirname fp
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -369,28 +368,6 @@ goUp' fp = readFile getFileInfo $ P.dirname fp
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
---- HANDLING FAILURES ----
|
|
||||||
|
|
||||||
|
|
||||||
-- |True if any Failed constructors in the tree.
|
|
||||||
anyFailed :: [File a] -> Bool
|
|
||||||
anyFailed = not . successful
|
|
||||||
|
|
||||||
-- |True if there are no Failed constructors in the tree.
|
|
||||||
successful :: [File a] -> Bool
|
|
||||||
successful = null . failures
|
|
||||||
|
|
||||||
|
|
||||||
-- |Returns true if argument is a `Failed` constructor.
|
|
||||||
failed :: File a -> Bool
|
|
||||||
failed (Failed _ _) = True
|
|
||||||
failed _ = False
|
|
||||||
|
|
||||||
|
|
||||||
-- |Returns a list of 'Failed' constructors only.
|
|
||||||
failures :: [File a] -> [File a]
|
|
||||||
failures = filter failed
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
---- ORDERING AND EQUALITY ----
|
---- ORDERING AND EQUALITY ----
|
||||||
@@ -398,11 +375,7 @@ failures = filter failed
|
|||||||
|
|
||||||
-- HELPER: a non-recursive comparison
|
-- HELPER: a non-recursive comparison
|
||||||
comparingConstr :: File FileInfo -> File FileInfo -> Ordering
|
comparingConstr :: File FileInfo -> File FileInfo -> Ordering
|
||||||
comparingConstr (Failed _ _) (DirOrSym _) = LT
|
|
||||||
comparingConstr (Failed _ _) (FileLikeOrSym _) = LT
|
|
||||||
comparingConstr (FileLikeOrSym _) (Failed _ _) = GT
|
|
||||||
comparingConstr (FileLikeOrSym _) (DirOrSym _) = GT
|
comparingConstr (FileLikeOrSym _) (DirOrSym _) = GT
|
||||||
comparingConstr (DirOrSym _) (Failed _ _) = GT
|
|
||||||
comparingConstr (DirOrSym _) (FileLikeOrSym _) = LT
|
comparingConstr (DirOrSym _) (FileLikeOrSym _) = LT
|
||||||
-- else compare on the names of constructors that are the same, without
|
-- else compare on the names of constructors that are the same, without
|
||||||
-- looking at the contents of Dir constructors:
|
-- looking at the contents of Dir constructors:
|
||||||
@@ -461,20 +434,6 @@ isSocketC _ = False
|
|||||||
---- IO HELPERS: ----
|
---- IO HELPERS: ----
|
||||||
|
|
||||||
|
|
||||||
-- |Gets all filenames of the given directory. This excludes "." and "..".
|
|
||||||
getDirsFiles :: Path Abs -- ^ dir to read
|
|
||||||
-> IO [Path Abs]
|
|
||||||
getDirsFiles p =
|
|
||||||
P.withAbsPath p $ \fp ->
|
|
||||||
rethrowErrnoAs [eACCES] (Can'tOpenDirectory fp)
|
|
||||||
$ return
|
|
||||||
. catMaybes
|
|
||||||
. fmap (\x -> (P.</>) p <$> (parseMaybe . snd $ x))
|
|
||||||
=<< getDirectoryContents fp
|
|
||||||
where
|
|
||||||
parseMaybe :: ByteString -> Maybe (Path Fn)
|
|
||||||
parseMaybe = P.parseFn
|
|
||||||
|
|
||||||
|
|
||||||
-- |Gets all file information.
|
-- |Gets all file information.
|
||||||
getFileInfo :: Path Abs -> IO FileInfo
|
getFileInfo :: Path Abs -> IO FileInfo
|
||||||
@@ -498,29 +457,6 @@ getFileInfo fp = do
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
---- FAILURE HELPERS: ----
|
|
||||||
|
|
||||||
|
|
||||||
-- Handles an IO exception by returning a Failed constructor filled with that
|
|
||||||
-- exception. Does not handle FmIOExceptions.
|
|
||||||
handleDT :: Path Abs
|
|
||||||
-> IO (File a)
|
|
||||||
-> IO (File a)
|
|
||||||
handleDT p
|
|
||||||
= handleIOError $ \e -> return $ Failed p e
|
|
||||||
|
|
||||||
|
|
||||||
-- DoesNotExist errors not present at the topmost level could happen if a
|
|
||||||
-- named file or directory is deleted after being listed by
|
|
||||||
-- getDirectoryContents but before we can get it into memory.
|
|
||||||
-- So we filter those errors out because the user should not see errors
|
|
||||||
-- raised by the internal implementation of this module:
|
|
||||||
-- This leaves the error if it exists in the top (user-supplied) level:
|
|
||||||
removeNonexistent :: [File a] -> [File a]
|
|
||||||
removeNonexistent = filter isOkConstructor
|
|
||||||
where
|
|
||||||
isOkConstructor c = not (failed c) || isOkError c
|
|
||||||
isOkError = not . isDoesNotExistErrorType . ioeGetErrorType . err
|
|
||||||
|
|
||||||
|
|
||||||
---- SYMLINK HELPERS: ----
|
---- SYMLINK HELPERS: ----
|
||||||
@@ -531,52 +467,40 @@ removeNonexistent = filter isOkConstructor
|
|||||||
--
|
--
|
||||||
-- When called on a non-symlink, returns False.
|
-- When called on a non-symlink, returns False.
|
||||||
isBrokenSymlink :: File FileInfo -> Bool
|
isBrokenSymlink :: File FileInfo -> Bool
|
||||||
isBrokenSymlink (SymLink _ _ Failed{} _) = True
|
isBrokenSymlink (SymLink _ _ Nothing _) = True
|
||||||
isBrokenSymlink _ = False
|
isBrokenSymlink _ = False
|
||||||
|
|
||||||
|
|
||||||
---- OTHER: ----
|
|
||||||
|
|
||||||
|
|
||||||
-- |Apply a function on the free variable. If there is no free variable
|
---- PACKERS: ----
|
||||||
-- for the given constructor the value from the `Default` class is used.
|
|
||||||
fromFreeVar :: (Default d) => (a -> d) -> File a -> d
|
|
||||||
fromFreeVar f df = maybeD f $ getFreeVar df
|
|
||||||
|
|
||||||
|
|
||||||
getFPasStr :: File a -> String
|
|
||||||
getFPasStr = P.fpToString . P.fromAbs . path
|
|
||||||
|
|
||||||
|
|
||||||
-- |Gets the free variable. Returns Nothing if the constructor is of `Failed`.
|
|
||||||
getFreeVar :: File a -> Maybe a
|
|
||||||
getFreeVar (Dir _ d) = Just d
|
|
||||||
getFreeVar (RegFile _ d) = Just d
|
|
||||||
getFreeVar (SymLink _ d _ _) = Just d
|
|
||||||
getFreeVar (BlockDev _ d) = Just d
|
|
||||||
getFreeVar (CharDev _ d) = Just d
|
|
||||||
getFreeVar (NamedPipe _ d) = Just d
|
|
||||||
getFreeVar (Socket _ d) = Just d
|
|
||||||
getFreeVar _ = Nothing
|
|
||||||
|
|
||||||
|
|
||||||
-- |Pack the modification time into a string.
|
-- |Pack the modification time into a string.
|
||||||
packModTime :: File FileInfo
|
packModTime :: File FileInfo
|
||||||
-> String
|
-> String
|
||||||
packModTime =
|
packModTime = epochToString . modificationTime . fvar
|
||||||
fromFreeVar
|
|
||||||
$ show . posixSecondsToUTCTime . realToFrac . modificationTime
|
|
||||||
|
-- |Pack the modification time into a string.
|
||||||
|
packAccessTime :: File FileInfo
|
||||||
|
-> String
|
||||||
|
packAccessTime = epochToString . accessTime . fvar
|
||||||
|
|
||||||
|
|
||||||
|
epochToString :: EpochTime -> String
|
||||||
|
epochToString = show . posixSecondsToUTCTime . realToFrac
|
||||||
|
|
||||||
|
|
||||||
-- |Pack the permissions into a string, similar to what "ls -l" does.
|
-- |Pack the permissions into a string, similar to what "ls -l" does.
|
||||||
packPermissions :: File FileInfo
|
packPermissions :: File FileInfo
|
||||||
-> String
|
-> String
|
||||||
packPermissions dt = fromFreeVar (pStr . fileMode) dt
|
packPermissions file = (pStr . fileMode) . fvar $ file
|
||||||
where
|
where
|
||||||
pStr :: FileMode -> String
|
pStr :: FileMode -> String
|
||||||
pStr ffm = typeModeStr ++ ownerModeStr ++ groupModeStr ++ otherModeStr
|
pStr ffm = typeModeStr ++ ownerModeStr ++ groupModeStr ++ otherModeStr
|
||||||
where
|
where
|
||||||
typeModeStr = case dt of
|
typeModeStr = case file of
|
||||||
Dir {} -> "d"
|
Dir {} -> "d"
|
||||||
RegFile {} -> "-"
|
RegFile {} -> "-"
|
||||||
SymLink {} -> "l"
|
SymLink {} -> "l"
|
||||||
@@ -584,7 +508,6 @@ packPermissions dt = fromFreeVar (pStr . fileMode) dt
|
|||||||
CharDev {} -> "c"
|
CharDev {} -> "c"
|
||||||
NamedPipe {} -> "p"
|
NamedPipe {} -> "p"
|
||||||
Socket {} -> "s"
|
Socket {} -> "s"
|
||||||
_ -> "?"
|
|
||||||
ownerModeStr = hasFmStr PF.ownerReadMode "r"
|
ownerModeStr = hasFmStr PF.ownerReadMode "r"
|
||||||
++ hasFmStr PF.ownerWriteMode "w"
|
++ hasFmStr PF.ownerWriteMode "w"
|
||||||
++ hasFmStr PF.ownerExecuteMode "x"
|
++ hasFmStr PF.ownerExecuteMode "x"
|
||||||
@@ -599,3 +522,29 @@ packPermissions dt = fromFreeVar (pStr . fileMode) dt
|
|||||||
| otherwise = "-"
|
| otherwise = "-"
|
||||||
hasFM fm = ffm `PF.intersectFileModes` fm == fm
|
hasFM fm = ffm `PF.intersectFileModes` fm == fm
|
||||||
|
|
||||||
|
|
||||||
|
packFileType :: File a -> String
|
||||||
|
packFileType file = case file of
|
||||||
|
Dir {} -> "Directory"
|
||||||
|
RegFile {} -> "Regular File"
|
||||||
|
SymLink {} -> "Symbolic Link"
|
||||||
|
BlockDev {} -> "Block Device"
|
||||||
|
CharDev {} -> "Char Device"
|
||||||
|
NamedPipe {} -> "Named Pipe"
|
||||||
|
Socket {} -> "Socket"
|
||||||
|
|
||||||
|
|
||||||
|
packLinkDestination :: File a -> Maybe ByteString
|
||||||
|
packLinkDestination file = case file of
|
||||||
|
SymLink { rawdest = dest } -> Just dest
|
||||||
|
_ -> Nothing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- OTHER: ----
|
||||||
|
|
||||||
|
|
||||||
|
getFPasStr :: File a -> String
|
||||||
|
getFPasStr = toString . P.fromAbs . path
|
||||||
|
|
||||||
|
|||||||
83
src/HSFM/FileSystem/UtilTypes.hs
Normal file
83
src/HSFM/FileSystem/UtilTypes.hs
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
|
|
||||||
|
-- |This module provides high-level IO related file operations like
|
||||||
|
-- copy, delete, move and so on. It only operates on `Path Abs` which
|
||||||
|
-- guarantees us well-typed paths which are absolute.
|
||||||
|
--
|
||||||
|
-- Some functions are just path-safe wrappers around
|
||||||
|
-- unix functions, others have stricter exception handling
|
||||||
|
-- and some implement functionality that doesn't have a unix
|
||||||
|
-- counterpart (like `copyDirRecursive`).
|
||||||
|
--
|
||||||
|
-- Some of these operations are due to their nature not _atomic_, which
|
||||||
|
-- means they may do multiple syscalls which form one context. Some
|
||||||
|
-- of them also have to examine the filetypes explicitly before the
|
||||||
|
-- syscalls, so a reasonable decision can be made. That means
|
||||||
|
-- the result is undefined if another process changes that context
|
||||||
|
-- while the non-atomic operation is still happening. However, where
|
||||||
|
-- possible, as few syscalls as possible are used and the underlying
|
||||||
|
-- exception handling is kept.
|
||||||
|
module HSFM.FileSystem.UtilTypes where
|
||||||
|
|
||||||
|
|
||||||
|
import Data.ByteString
|
||||||
|
(
|
||||||
|
ByteString
|
||||||
|
)
|
||||||
|
import HPath
|
||||||
|
(
|
||||||
|
Path
|
||||||
|
, Abs
|
||||||
|
, Rel
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
-- |Data type describing file operations.
|
||||||
|
-- Useful to build up a list of operations or delay operations.
|
||||||
|
data FileOperation = FCopy Copy
|
||||||
|
| FMove Move
|
||||||
|
| FDelete [Path Abs]
|
||||||
|
| FOpen (Path Abs)
|
||||||
|
| FExecute (Path Abs) [ByteString]
|
||||||
|
| None
|
||||||
|
|
||||||
|
|
||||||
|
-- |Data type describing partial or complete file copy operation.
|
||||||
|
data Copy = PartialCopy [Path Abs] -- source files
|
||||||
|
| Copy [Path Abs] -- source files
|
||||||
|
(Path Abs) -- base destination directory
|
||||||
|
|
||||||
|
|
||||||
|
-- |Data type describing partial or complete file move operation.
|
||||||
|
data Move = PartialMove [Path Abs] -- source files
|
||||||
|
| Move [Path Abs] -- source files
|
||||||
|
(Path Abs) -- base destination directory
|
||||||
|
|
||||||
|
|
||||||
|
-- |Collision modes that describe the behavior in case a file collision
|
||||||
|
-- happens.
|
||||||
|
data FCollisonMode = Strict -- ^ fail if the target already exists
|
||||||
|
| Overwrite
|
||||||
|
| OverwriteAll
|
||||||
|
| Skip
|
||||||
|
| Rename (Path Rel)
|
||||||
|
|
||||||
@@ -16,7 +16,6 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
|
|
||||||
@@ -67,7 +66,7 @@ instance GlibString BS.ByteString where
|
|||||||
newUTFStringLen = newUTFStringLen . toString
|
newUTFStringLen = newUTFStringLen . toString
|
||||||
genUTFOfs = genUTFOfs . toString
|
genUTFOfs = genUTFOfs . toString
|
||||||
stringLength = BS.length
|
stringLength = BS.length
|
||||||
unPrintf s = BS.intercalate "%%" (BS.split _percent s)
|
unPrintf s = BS.intercalate (BS.pack [_percent, _percent]) (BS.split _percent s)
|
||||||
|
|
||||||
|
|
||||||
foreign import ccall unsafe "string.h strlen" c_strlen
|
foreign import ccall unsafe "string.h strlen" c_strlen
|
||||||
|
|||||||
@@ -16,42 +16,54 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
|
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
import Data.Maybe
|
import Data.Maybe
|
||||||
(
|
(
|
||||||
fromJust
|
fromJust
|
||||||
, fromMaybe
|
, fromMaybe
|
||||||
)
|
)
|
||||||
|
import Data.Word8
|
||||||
import Graphics.UI.Gtk
|
import Graphics.UI.Gtk
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
|
import HSFM.FileSystem.FileType
|
||||||
|
import HSFM.GUI.Gtk.Callbacks
|
||||||
import HSFM.GUI.Gtk.Data
|
import HSFM.GUI.Gtk.Data
|
||||||
import HSFM.GUI.Gtk.MyGUI
|
import HSFM.GUI.Gtk.MyGUI
|
||||||
import HSFM.GUI.Gtk.MyView
|
import HSFM.GUI.Gtk.MyView
|
||||||
|
import Prelude hiding(readFile)
|
||||||
import Safe
|
import Safe
|
||||||
(
|
(
|
||||||
headDef
|
headDef
|
||||||
)
|
)
|
||||||
|
import System.IO.Error
|
||||||
|
(
|
||||||
|
catchIOError
|
||||||
|
)
|
||||||
import qualified System.Posix.Env.ByteString as SPE
|
import qualified System.Posix.Env.ByteString as SPE
|
||||||
|
|
||||||
|
slash :: BS.ByteString
|
||||||
|
slash = BS.singleton _slash
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = do
|
main = do
|
||||||
_ <- initGUI
|
|
||||||
|
|
||||||
args <- SPE.getArgs
|
args <- SPE.getArgs
|
||||||
|
let mdir = fromMaybe (fromJust $ P.parseAbs slash)
|
||||||
|
(P.parseAbs . headDef slash $ args)
|
||||||
|
|
||||||
|
file <- catchIOError (pathToFile getFileInfo mdir) $
|
||||||
|
\_ -> pathToFile getFileInfo . fromJust $ P.parseAbs slash
|
||||||
|
|
||||||
|
_ <- initGUI
|
||||||
mygui <- createMyGUI
|
mygui <- createMyGUI
|
||||||
|
_ <- newTab mygui (notebook1 mygui) createTreeView file (-1)
|
||||||
|
_ <- newTab mygui (notebook2 mygui) createTreeView file (-1)
|
||||||
|
|
||||||
myview <- createMyView mygui createTreeView
|
setGUICallbacks mygui
|
||||||
|
|
||||||
let mdir = fromMaybe (fromJust $ P.parseAbs "/")
|
|
||||||
(P.parseAbs . headDef "/" $ args)
|
|
||||||
refreshView mygui myview (Just $ mdir)
|
|
||||||
|
|
||||||
widgetShowAll (rootWin mygui)
|
widgetShowAll (rootWin mygui)
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE TupleSections #-}
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
module HSFM.GUI.Gtk.Callbacks where
|
module HSFM.GUI.Gtk.Callbacks where
|
||||||
@@ -28,17 +28,34 @@ import Control.Concurrent.STM
|
|||||||
)
|
)
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
(
|
(
|
||||||
throw
|
throwIO
|
||||||
)
|
)
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
(
|
(
|
||||||
void
|
forM
|
||||||
, forM_
|
, forM_
|
||||||
|
, join
|
||||||
|
, void
|
||||||
|
, when
|
||||||
)
|
)
|
||||||
|
import Control.Monad.IfElse
|
||||||
import Control.Monad.IO.Class
|
import Control.Monad.IO.Class
|
||||||
(
|
(
|
||||||
liftIO
|
liftIO
|
||||||
)
|
)
|
||||||
|
import Control.Monad.Loops
|
||||||
|
(
|
||||||
|
iterateUntil
|
||||||
|
)
|
||||||
|
import Data.ByteString
|
||||||
|
(
|
||||||
|
ByteString
|
||||||
|
)
|
||||||
|
import Data.ByteString.UTF8
|
||||||
|
(
|
||||||
|
fromString
|
||||||
|
, toString
|
||||||
|
)
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
(
|
(
|
||||||
for_
|
for_
|
||||||
@@ -46,26 +63,44 @@ import Data.Foldable
|
|||||||
import Graphics.UI.Gtk
|
import Graphics.UI.Gtk
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
import HPath
|
import HPath
|
||||||
(
|
(
|
||||||
Abs
|
fromAbs
|
||||||
, Path
|
, Abs
|
||||||
)
|
, Path
|
||||||
import HSFM.FileSystem.Errors
|
)
|
||||||
import HSFM.FileSystem.FileOperations
|
import HPath.IO
|
||||||
|
import HPath.IO.Errors
|
||||||
import HSFM.FileSystem.FileType
|
import HSFM.FileSystem.FileType
|
||||||
|
import HSFM.FileSystem.UtilTypes
|
||||||
|
import HSFM.GUI.Gtk.Callbacks.Utils
|
||||||
import HSFM.GUI.Gtk.Data
|
import HSFM.GUI.Gtk.Data
|
||||||
import HSFM.GUI.Gtk.Dialogs
|
import HSFM.GUI.Gtk.Dialogs
|
||||||
import HSFM.GUI.Gtk.MyView
|
import HSFM.GUI.Gtk.MyView
|
||||||
|
import HSFM.GUI.Gtk.Plugins
|
||||||
|
import HSFM.GUI.Gtk.Settings
|
||||||
import HSFM.GUI.Gtk.Utils
|
import HSFM.GUI.Gtk.Utils
|
||||||
|
import HSFM.History
|
||||||
|
import HSFM.Settings
|
||||||
import HSFM.Utils.IO
|
import HSFM.Utils.IO
|
||||||
import Prelude hiding(readFile)
|
import Prelude hiding(readFile)
|
||||||
import System.Glib.UTFString
|
import System.Glib.UTFString
|
||||||
(
|
(
|
||||||
glibToString
|
glibToString
|
||||||
)
|
)
|
||||||
import System.Posix.Env.ByteString
|
import qualified System.Posix.Process.ByteString as SPP
|
||||||
|
import System.Posix.Types
|
||||||
(
|
(
|
||||||
getEnv
|
ProcessID
|
||||||
|
)
|
||||||
|
import Control.Concurrent.MVar
|
||||||
|
(
|
||||||
|
putMVar
|
||||||
|
, readMVar
|
||||||
|
, takeMVar
|
||||||
|
)
|
||||||
|
import Paths_hsfm
|
||||||
|
(
|
||||||
|
getDataFileName
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -76,14 +111,87 @@ import System.Posix.Env.ByteString
|
|||||||
-----------------
|
-----------------
|
||||||
|
|
||||||
|
|
||||||
-- |Set callbacks, on hotkeys, events and stuff.
|
|
||||||
setCallbacks :: MyGUI -> MyView -> IO ()
|
|
||||||
setCallbacks mygui myview = do
|
---- MAIN CALLBACK ENTRYPOINT ----
|
||||||
|
|
||||||
|
|
||||||
|
-- |Set callbacks for the whole gui, on hotkeys, events and stuff.
|
||||||
|
setGUICallbacks :: MyGUI -> IO ()
|
||||||
|
setGUICallbacks mygui = do
|
||||||
|
|
||||||
|
-- notebook toggle buttons
|
||||||
|
_ <- leftNbBtn mygui `on` toggled $ do
|
||||||
|
isPressed <- toggleButtonGetActive $ leftNbBtn mygui
|
||||||
|
if isPressed then widgetShow $ notebook1 mygui
|
||||||
|
else widgetHide $ notebook1 mygui
|
||||||
|
|
||||||
|
_ <- rightNbBtn mygui `on` toggled $ do
|
||||||
|
isPressed <- toggleButtonGetActive $ rightNbBtn mygui
|
||||||
|
if isPressed then widgetShow $ notebook2 mygui
|
||||||
|
else widgetHide $ notebook2 mygui
|
||||||
|
|
||||||
|
-- statusbar
|
||||||
|
_ <- clearStatusBar mygui `on` buttonActivated $ do
|
||||||
|
popStatusbar mygui
|
||||||
|
writeTVarIO (operationBuffer mygui) None
|
||||||
|
|
||||||
|
-- menubar-file
|
||||||
|
_ <- (menubarFileQuit . menubar) mygui `on` menuItemActivated $
|
||||||
|
mainQuit
|
||||||
|
|
||||||
|
-- menubar-help
|
||||||
|
_ <- (menubarHelpAbout . menubar) mygui `on` menuItemActivated $
|
||||||
|
liftIO showAboutDialog
|
||||||
|
return ()
|
||||||
|
|
||||||
|
-- key events
|
||||||
|
_ <- rootWin mygui `on` keyPressEvent $ tryEvent $ do
|
||||||
|
QuitModifier <- eventModifier
|
||||||
|
QuitKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO mainQuit
|
||||||
|
|
||||||
|
return ()
|
||||||
|
|
||||||
|
|
||||||
|
-- |Set callbacks specific to a given view, on hotkeys, events and stuff.
|
||||||
|
setViewCallbacks :: MyGUI -> MyView -> IO ()
|
||||||
|
setViewCallbacks mygui myview = do
|
||||||
view' <- readTVarIO $ view myview
|
view' <- readTVarIO $ view myview
|
||||||
case view' of
|
case view' of
|
||||||
fmv@(FMTreeView treeView) -> do
|
fmv@(FMTreeView treeView) -> do
|
||||||
_ <- treeView `on` rowActivated
|
_ <- treeView `on` rowActivated
|
||||||
$ (\_ _ -> withItems mygui myview open)
|
$ (\_ _ -> withItems mygui myview open)
|
||||||
|
|
||||||
|
-- drag events
|
||||||
|
_ <- treeView `on` dragBegin $
|
||||||
|
\_ -> withItems mygui myview moveInit
|
||||||
|
_ <- treeView `on` dragDrop $
|
||||||
|
\dc p ts -> do
|
||||||
|
p' <- treeViewConvertWidgetToTreeCoords treeView p
|
||||||
|
mpath <- treeViewGetPathAtPos treeView p'
|
||||||
|
case mpath of
|
||||||
|
Nothing -> do
|
||||||
|
dragFinish dc False False ts
|
||||||
|
return False
|
||||||
|
Just _ -> do
|
||||||
|
atom <- atomNew ("HSFM" :: String)
|
||||||
|
dragGetData treeView dc atom ts
|
||||||
|
return True
|
||||||
|
_ <- treeView `on` dragDataReceived $
|
||||||
|
\dc p _ ts ->
|
||||||
|
liftIO $ do
|
||||||
|
signalStopEmission treeView "drag_data_received"
|
||||||
|
p' <- treeViewConvertWidgetToTreeCoords treeView p
|
||||||
|
mpath <- treeViewGetPathAtPos treeView p'
|
||||||
|
case mpath of
|
||||||
|
Nothing -> dragFinish dc False False ts
|
||||||
|
Just (tp, _, _) -> do
|
||||||
|
mitem <- rawPathToItem myview tp
|
||||||
|
forM_ mitem $ \item ->
|
||||||
|
operationFinal mygui myview (Just item)
|
||||||
|
dragFinish dc True False ts
|
||||||
|
|
||||||
commonGuiEvents fmv
|
commonGuiEvents fmv
|
||||||
return ()
|
return ()
|
||||||
fmv@(FMIconView iconView) -> do
|
fmv@(FMIconView iconView) -> do
|
||||||
@@ -91,101 +199,118 @@ setCallbacks mygui myview = do
|
|||||||
$ (\_ -> withItems mygui myview open)
|
$ (\_ -> withItems mygui myview open)
|
||||||
commonGuiEvents fmv
|
commonGuiEvents fmv
|
||||||
return ()
|
return ()
|
||||||
menubarCallbacks
|
|
||||||
where
|
where
|
||||||
menubarCallbacks = do
|
|
||||||
-- menubar-file
|
|
||||||
_ <- menubarFileQuit mygui `on` menuItemActivated $ mainQuit
|
|
||||||
_ <- menubarFileOpen mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview open
|
|
||||||
_ <- menubarFileExecute mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview execute
|
|
||||||
_ <- menubarFileNew mygui `on` menuItemActivated $
|
|
||||||
liftIO $ newFile mygui myview
|
|
||||||
|
|
||||||
-- menubar-edit
|
|
||||||
_ <- menubarEditCut mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview moveInit
|
|
||||||
_ <- menubarEditCopy mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview copyInit
|
|
||||||
_ <- menubarEditRename mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview renameF
|
|
||||||
_ <- menubarEditPaste mygui `on` menuItemActivated $
|
|
||||||
liftIO $ operationFinal mygui myview
|
|
||||||
_ <- menubarEditDelete mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview del
|
|
||||||
|
|
||||||
-- mewnubar-view
|
|
||||||
_ <- menubarViewIcon mygui `on` menuItemActivated $
|
|
||||||
liftIO $ switchView mygui myview createIconView
|
|
||||||
_ <- menubarViewTree mygui `on` menuItemActivated $
|
|
||||||
liftIO $ switchView mygui myview createTreeView
|
|
||||||
|
|
||||||
-- menubar-help
|
|
||||||
_ <- menubarHelpAbout mygui `on` menuItemActivated $
|
|
||||||
liftIO showAboutDialog
|
|
||||||
return ()
|
|
||||||
commonGuiEvents fmv = do
|
commonGuiEvents fmv = do
|
||||||
let view = fmViewToContainer fmv
|
let view = fmViewToContainer fmv
|
||||||
|
|
||||||
-- GUI events
|
-- focus events
|
||||||
_ <- urlBar mygui `on` entryActivated $ urlGoTo mygui myview
|
_ <- notebook1 mygui `on` setFocusChild $ \w ->
|
||||||
|
case w of
|
||||||
|
Nothing -> widgetSetSensitive (leftNbIcon mygui) False
|
||||||
|
_ -> widgetSetSensitive (leftNbIcon mygui) True
|
||||||
|
_ <- notebook2 mygui `on` setFocusChild $ \w ->
|
||||||
|
case w of
|
||||||
|
Nothing -> widgetSetSensitive (rightNbIcon mygui) False
|
||||||
|
_ -> widgetSetSensitive (rightNbIcon mygui) True
|
||||||
|
|
||||||
_ <- upViewB mygui `on` buttonActivated $
|
-- GUI events
|
||||||
|
_ <- backViewB myview `on` buttonPressEvent $ do
|
||||||
|
eb <- eventButton
|
||||||
|
t <- eventTime
|
||||||
|
case eb of
|
||||||
|
LeftButton -> do
|
||||||
|
liftIO $ void $ goHistoryBack mygui myview
|
||||||
|
return True
|
||||||
|
RightButton -> do
|
||||||
|
his <- liftIO $ readMVar (history myview)
|
||||||
|
menu <- liftIO $ mkHistoryMenuB mygui myview
|
||||||
|
(backwardsHistory his)
|
||||||
|
_ <- liftIO $ menuPopup menu $ Just (RightButton, t)
|
||||||
|
return True
|
||||||
|
_ -> return False
|
||||||
|
_ <- forwardViewB myview `on` buttonPressEvent $ do
|
||||||
|
eb <- eventButton
|
||||||
|
t <- eventTime
|
||||||
|
case eb of
|
||||||
|
LeftButton -> do
|
||||||
|
liftIO $ void $ goHistoryForward mygui myview
|
||||||
|
return True
|
||||||
|
RightButton -> do
|
||||||
|
his <- liftIO $ readMVar (history myview)
|
||||||
|
menu <- liftIO $ mkHistoryMenuF mygui myview
|
||||||
|
(forwardHistory his)
|
||||||
|
_ <- liftIO $ menuPopup menu $ Just (RightButton, t)
|
||||||
|
return True
|
||||||
|
_ -> return False
|
||||||
|
_ <- urlBar myview `on` entryActivated $ urlGoTo mygui myview
|
||||||
|
_ <- upViewB myview `on` buttonActivated $
|
||||||
upDir mygui myview
|
upDir mygui myview
|
||||||
_ <- homeViewB mygui `on` buttonActivated $
|
_ <- homeViewB myview `on` buttonActivated $
|
||||||
goHome mygui myview
|
goHome mygui myview
|
||||||
_ <- refreshViewB mygui `on` buttonActivated $ do
|
_ <- refreshViewB myview `on` buttonActivated $ do
|
||||||
cdir <- liftIO $ getCurrentDir myview
|
cdir <- liftIO $ getCurrentDir myview
|
||||||
refreshView' mygui myview cdir
|
refreshView mygui myview cdir
|
||||||
_ <- clearStatusBar mygui `on` buttonActivated $ do
|
|
||||||
popStatusbar mygui
|
|
||||||
writeTVarIO (operationBuffer myview) None
|
|
||||||
|
|
||||||
-- key events
|
-- key events
|
||||||
_ <- rootWin mygui `on` keyPressEvent $ tryEvent $ do
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
[Control] <- eventModifier
|
ShowHiddenModifier <- eventModifier
|
||||||
"q" <- fmap glibToString eventKeyName
|
ShowHiddenKey <- fmap glibToString eventKeyName
|
||||||
liftIO mainQuit
|
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
|
||||||
[Control] <- eventModifier
|
|
||||||
"h" <- fmap glibToString eventKeyName
|
|
||||||
cdir <- liftIO $ getCurrentDir myview
|
cdir <- liftIO $ getCurrentDir myview
|
||||||
liftIO $ modifyTVarIO (settings mygui)
|
liftIO $ modifyTVarIO (settings mygui)
|
||||||
(\x -> x { showHidden = not . showHidden $ x})
|
(\x -> x { showHidden = not . showHidden $ x})
|
||||||
>> refreshView' mygui myview cdir
|
>> refreshView mygui myview cdir
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
[Alt] <- eventModifier
|
UpDirModifier <- eventModifier
|
||||||
"Up" <- fmap glibToString eventKeyName
|
UpDirKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ upDir mygui myview
|
liftIO $ upDir mygui myview
|
||||||
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
|
HistoryBackModifier <- eventModifier
|
||||||
|
HistoryBackKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO $ void $ goHistoryBack mygui myview
|
||||||
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
|
HistoryForwardModifier <- eventModifier
|
||||||
|
HistoryForwardKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO $ void $ goHistoryForward mygui myview
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- view `on` keyPressEvent $ tryEvent $ do
|
||||||
"Delete" <- fmap glibToString eventKeyName
|
DeleteModifier <- eventModifier
|
||||||
|
DeleteKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ withItems mygui myview del
|
liftIO $ withItems mygui myview del
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- view `on` keyPressEvent $ tryEvent $ do
|
||||||
[] <- eventModifier
|
OpenModifier <- eventModifier
|
||||||
"Return" <- fmap glibToString eventKeyName
|
OpenKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ withItems mygui myview open
|
liftIO $ withItems mygui myview open
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- view `on` keyPressEvent $ tryEvent $ do
|
||||||
[Control] <- eventModifier
|
CopyModifier <- eventModifier
|
||||||
"c" <- fmap glibToString eventKeyName
|
CopyKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ withItems mygui myview copyInit
|
liftIO $ withItems mygui myview copyInit
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- view `on` keyPressEvent $ tryEvent $ do
|
||||||
[Control] <- eventModifier
|
MoveModifier <- eventModifier
|
||||||
"x" <- fmap glibToString eventKeyName
|
MoveKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ withItems mygui myview moveInit
|
liftIO $ withItems mygui myview moveInit
|
||||||
_ <- view `on` keyPressEvent $ tryEvent $ do
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
[Control] <- eventModifier
|
PasteModifier <- eventModifier
|
||||||
"v" <- fmap glibToString eventKeyName
|
PasteKey <- fmap glibToString eventKeyName
|
||||||
liftIO $ operationFinal mygui myview
|
liftIO $ operationFinal mygui myview Nothing
|
||||||
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
|
NewTabModifier <- eventModifier
|
||||||
|
NewTabKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO $ void $ newTab' mygui myview
|
||||||
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
|
CloseTabModifier <- eventModifier
|
||||||
|
CloseTabKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO $ void $ closeTab mygui myview
|
||||||
|
_ <- viewBox myview `on` keyPressEvent $ tryEvent $ do
|
||||||
|
OpenTerminalModifier <- eventModifier
|
||||||
|
OpenTerminalKey <- fmap glibToString eventKeyName
|
||||||
|
liftIO $ void $ openTerminalHere myview
|
||||||
|
|
||||||
-- righ-click
|
-- mouse button click
|
||||||
_ <- view `on` buttonPressEvent $ do
|
_ <- view `on` buttonPressEvent $ do
|
||||||
eb <- eventButton
|
eb <- eventButton
|
||||||
t <- eventTime
|
t <- eventTime
|
||||||
case eb of
|
case eb of
|
||||||
RightButton -> do
|
RightButton -> do
|
||||||
_ <- liftIO $ menuPopup (rcMenu mygui)
|
_ <- liftIO $ showPopup mygui myview t
|
||||||
$ Just (RightButton, t)
|
|
||||||
-- this is just to not screw with current selection
|
-- this is just to not screw with current selection
|
||||||
-- on right-click
|
-- on right-click
|
||||||
-- TODO: this misbehaves under IconView
|
-- TODO: this misbehaves under IconView
|
||||||
@@ -200,24 +325,31 @@ setCallbacks mygui myview = do
|
|||||||
return $ elem tp selectedTps
|
return $ elem tp selectedTps
|
||||||
-- no item under the cursor, pass on the signal
|
-- no item under the cursor, pass on the signal
|
||||||
Nothing -> return False
|
Nothing -> return False
|
||||||
|
MiddleButton -> do
|
||||||
|
(x, y) <- eventCoordinates
|
||||||
|
mitem <- liftIO $ (getPathAtPos fmv (x, y))
|
||||||
|
>>= \mpos -> fmap join
|
||||||
|
$ forM mpos (rawPathToItem myview)
|
||||||
|
|
||||||
|
case mitem of
|
||||||
|
-- item under the cursor, only pass on the signal
|
||||||
|
-- if the item under the cursor is not within the current
|
||||||
|
-- selection
|
||||||
|
(Just item) -> do
|
||||||
|
liftIO $ opeInNewTab mygui myview item
|
||||||
|
return True
|
||||||
|
-- no item under the cursor, pass on the signal
|
||||||
|
Nothing -> return False
|
||||||
|
|
||||||
|
OtherButton 8 -> do
|
||||||
|
liftIO $ void $ goHistoryBack mygui myview
|
||||||
|
return False
|
||||||
|
OtherButton 9 -> do
|
||||||
|
liftIO $ void $ goHistoryForward mygui myview
|
||||||
|
return False
|
||||||
-- not right-click, so pass on the signal
|
-- not right-click, so pass on the signal
|
||||||
_ -> return False
|
_ -> return False
|
||||||
_ <- rcFileOpen mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview open
|
|
||||||
_ <- rcFileExecute mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview execute
|
|
||||||
_ <- rcFileNew mygui `on` menuItemActivated $
|
|
||||||
liftIO $ newFile mygui myview
|
|
||||||
_ <- rcFileCopy mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview copyInit
|
|
||||||
_ <- rcFileRename mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview renameF
|
|
||||||
_ <- rcFilePaste mygui `on` menuItemActivated $
|
|
||||||
liftIO $ operationFinal mygui myview
|
|
||||||
_ <- rcFileDelete mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview del
|
|
||||||
_ <- rcFileCut mygui `on` menuItemActivated $
|
|
||||||
liftIO $ withItems mygui myview moveInit
|
|
||||||
return ()
|
return ()
|
||||||
getPathAtPos fmv (x, y) =
|
getPathAtPos fmv (x, y) =
|
||||||
case fmv of
|
case fmv of
|
||||||
@@ -230,47 +362,44 @@ setCallbacks mygui myview = do
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- |Go to the url given at the 'urlBar' and visualize it in the given
|
|
||||||
-- treeView.
|
---- OTHER ----
|
||||||
--
|
|
||||||
-- If the url is invalid, does nothing.
|
|
||||||
urlGoTo :: MyGUI -> MyView -> IO ()
|
|
||||||
urlGoTo mygui myview = withErrorDialog $ do
|
|
||||||
fp <- entryGetText (urlBar mygui)
|
|
||||||
forM_ (P.parseAbs fp :: Maybe (Path Abs)) $ \fp' ->
|
|
||||||
refreshView mygui myview (Just fp')
|
|
||||||
|
|
||||||
|
|
||||||
goHome :: MyGUI -> MyView -> IO ()
|
openTerminalHere :: MyView -> IO ProcessID
|
||||||
goHome mygui myview = withErrorDialog $ do
|
openTerminalHere myview = do
|
||||||
mhomedir <- getEnv "HOME"
|
cwd <- (P.fromAbs . path) <$> getCurrentDir myview
|
||||||
refreshView mygui myview (P.parseAbs =<< mhomedir)
|
SPP.forkProcess $ terminalCommand cwd
|
||||||
|
|
||||||
|
|
||||||
-- |Supposed to be used with 'withRows'. Opens a file or directory.
|
|
||||||
open :: [Item] -> MyGUI -> MyView -> IO ()
|
|
||||||
open [item] mygui myview = withErrorDialog $
|
|
||||||
case item of
|
|
||||||
DirOrSym r -> do
|
|
||||||
nv <- readFile getFileInfo $ path r
|
|
||||||
refreshView' mygui myview nv
|
|
||||||
r ->
|
|
||||||
void $ openFile r
|
|
||||||
-- this throws on the first error that occurs
|
|
||||||
open (FileLikeList fs) _ _ = withErrorDialog $
|
|
||||||
forM_ fs $ \f -> void $ openFile f
|
|
||||||
open _ _ _ = withErrorDialog
|
|
||||||
. throw $ InvalidOperation
|
|
||||||
"Operation not supported on multiple files"
|
|
||||||
|
|
||||||
|
|
||||||
-- |Execute a given file.
|
---- TAB OPERATIONS ----
|
||||||
execute :: [Item] -> MyGUI -> MyView -> IO ()
|
|
||||||
execute [item] _ _ = withErrorDialog $
|
|
||||||
void $ executeFile item []
|
-- |Closes the current tab, but only if there is more than one tab.
|
||||||
execute _ _ _ = withErrorDialog
|
closeTab :: MyGUI -> MyView -> IO ()
|
||||||
. throw $ InvalidOperation
|
closeTab _ myview = do
|
||||||
"Operation not supported on multiple files"
|
n <- notebookGetNPages (notebook myview)
|
||||||
|
when (n > 1) $ void $ destroyView myview
|
||||||
|
|
||||||
|
|
||||||
|
newTab' :: MyGUI -> MyView -> IO ()
|
||||||
|
newTab' mygui myview = do
|
||||||
|
cwd <- getCurrentDir myview
|
||||||
|
void $ withErrorDialog
|
||||||
|
$ newTab mygui (notebook myview) createTreeView cwd (-1)
|
||||||
|
|
||||||
|
|
||||||
|
opeInNewTab :: MyGUI -> MyView -> Item -> IO ()
|
||||||
|
opeInNewTab mygui myview item@(DirOrSym _) =
|
||||||
|
void $ withErrorDialog
|
||||||
|
$ newTab mygui (notebook myview) createTreeView item (-1)
|
||||||
|
opeInNewTab _ _ _ = return ()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- FILE OPERATION CALLBACKS (COPY, MOVE, ...) ----
|
||||||
|
|
||||||
|
|
||||||
-- |Supposed to be used with 'withRows'. Deletes a file or directory.
|
-- |Supposed to be used with 'withRows'. Deletes a file or directory.
|
||||||
@@ -278,21 +407,21 @@ del :: [Item] -> MyGUI -> MyView -> IO ()
|
|||||||
del [item] _ _ = withErrorDialog $ do
|
del [item] _ _ = withErrorDialog $ do
|
||||||
let cmsg = "Really delete \"" ++ getFPasStr item ++ "\"?"
|
let cmsg = "Really delete \"" ++ getFPasStr item ++ "\"?"
|
||||||
withConfirmationDialog cmsg
|
withConfirmationDialog cmsg
|
||||||
$ easyDelete item
|
$ easyDelete . path $ item
|
||||||
-- this throws on the first error that occurs
|
-- this throws on the first error that occurs
|
||||||
del items@(_:_) _ _ = withErrorDialog $ do
|
del items@(_:_) _ _ = withErrorDialog $ do
|
||||||
let cmsg = "Really delete " ++ show (length items) ++ " files?"
|
let cmsg = "Really delete " ++ show (length items) ++ " files?"
|
||||||
withConfirmationDialog cmsg
|
withConfirmationDialog cmsg
|
||||||
$ forM_ items $ \item -> easyDelete item
|
$ forM_ items $ \item -> easyDelete . path $ item
|
||||||
del _ _ _ = withErrorDialog
|
del _ _ _ = withErrorDialog
|
||||||
. throw $ InvalidOperation
|
. ioError $ userError
|
||||||
"Operation not supported on multiple files"
|
"Operation not supported on multiple files"
|
||||||
|
|
||||||
|
|
||||||
-- |Initializes a file move operation.
|
-- |Initializes a file move operation.
|
||||||
moveInit :: [Item] -> MyGUI -> MyView -> IO ()
|
moveInit :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
moveInit items@(_:_) mygui myview = do
|
moveInit items@(_:_) mygui _ = do
|
||||||
writeTVarIO (operationBuffer myview) (FMove . MP1 . map path $ items)
|
writeTVarIO (operationBuffer mygui) (FMove . PartialMove . map path $ items)
|
||||||
let sbmsg = case items of
|
let sbmsg = case items of
|
||||||
(item:[]) -> "Move buffer: " ++ getFPasStr item
|
(item:[]) -> "Move buffer: " ++ getFPasStr item
|
||||||
_ -> "Move buffer: " ++ (show . length $ items)
|
_ -> "Move buffer: " ++ (show . length $ items)
|
||||||
@@ -300,13 +429,13 @@ moveInit items@(_:_) mygui myview = do
|
|||||||
popStatusbar mygui
|
popStatusbar mygui
|
||||||
void $ pushStatusBar mygui sbmsg
|
void $ pushStatusBar mygui sbmsg
|
||||||
moveInit _ _ _ = withErrorDialog
|
moveInit _ _ _ = withErrorDialog
|
||||||
. throw $ InvalidOperation
|
. ioError $ userError
|
||||||
"No file selected!"
|
"No file selected!"
|
||||||
|
|
||||||
-- |Supposed to be used with 'withRows'. Initializes a file copy operation.
|
-- |Supposed to be used with 'withRows'. Initializes a file copy operation.
|
||||||
copyInit :: [Item] -> MyGUI -> MyView -> IO ()
|
copyInit :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
copyInit items@(_:_) mygui myview = do
|
copyInit items@(_:_) mygui _ = do
|
||||||
writeTVarIO (operationBuffer myview) (FCopy . CP1 . map path $ items)
|
writeTVarIO (operationBuffer mygui) (FCopy . PartialCopy . map path $ items)
|
||||||
let sbmsg = case items of
|
let sbmsg = case items of
|
||||||
(item:[]) -> "Copy buffer: " ++ getFPasStr item
|
(item:[]) -> "Copy buffer: " ++ getFPasStr item
|
||||||
_ -> "Copy buffer: " ++ (show . length $ items)
|
_ -> "Copy buffer: " ++ (show . length $ items)
|
||||||
@@ -314,66 +443,286 @@ copyInit items@(_:_) mygui myview = do
|
|||||||
popStatusbar mygui
|
popStatusbar mygui
|
||||||
void $ pushStatusBar mygui sbmsg
|
void $ pushStatusBar mygui sbmsg
|
||||||
copyInit _ _ _ = withErrorDialog
|
copyInit _ _ _ = withErrorDialog
|
||||||
. throw $ InvalidOperation
|
. ioError $ userError
|
||||||
"No file selected!"
|
"No file selected!"
|
||||||
|
|
||||||
|
|
||||||
-- |Finalizes a file operation, such as copy or move.
|
-- |Finalizes a file operation, such as copy or move.
|
||||||
operationFinal :: MyGUI -> MyView -> IO ()
|
operationFinal :: MyGUI -> MyView -> Maybe Item -> IO ()
|
||||||
operationFinal _ myview = withErrorDialog $ do
|
operationFinal mygui myview mitem = withErrorDialog $ do
|
||||||
op <- readTVarIO (operationBuffer myview)
|
op <- readTVarIO (operationBuffer mygui)
|
||||||
cdir <- path <$> getCurrentDir myview
|
cdir <- case mitem of
|
||||||
|
Nothing -> path <$> getCurrentDir myview
|
||||||
|
Just x -> return $ path x
|
||||||
case op of
|
case op of
|
||||||
FMove (MP1 s) -> do
|
FMove (PartialMove s) -> do
|
||||||
let cmsg = "Really move " ++ imsg s
|
let cmsg = "Really move " ++ imsg s
|
||||||
++ " to \"" ++ P.fpToString (P.fromAbs cdir)
|
++ " to \"" ++ toString (P.fromAbs cdir)
|
||||||
++ "\"?"
|
++ "\"?"
|
||||||
withConfirmationDialog cmsg . withCopyModeDialog
|
withConfirmationDialog cmsg $ doFileOperation (FMove $ Move s cdir)
|
||||||
$ \cm -> void $ runFileOp (FMove . MC s cdir $ cm)
|
popStatusbar mygui
|
||||||
return ()
|
writeTVarIO (operationBuffer mygui) None
|
||||||
FCopy (CP1 s) -> do
|
FCopy (PartialCopy s) -> do
|
||||||
let cmsg = "Really copy " ++ imsg s
|
let cmsg = "Really copy " ++ imsg s
|
||||||
++ " to \"" ++ P.fpToString (P.fromAbs cdir)
|
++ " to \"" ++ toString (P.fromAbs cdir)
|
||||||
++ "\"?"
|
++ "\"?"
|
||||||
withConfirmationDialog cmsg . withCopyModeDialog
|
withConfirmationDialog cmsg $ doFileOperation (FCopy $ Copy s cdir)
|
||||||
$ \cm -> void $ runFileOp (FCopy . CC s cdir $ cm)
|
|
||||||
return ()
|
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
where
|
where
|
||||||
imsg s = case s of
|
imsg s = case s of
|
||||||
(item:[]) -> "\"" ++ P.fpToString (P.fromAbs item) ++ "\""
|
(item:[]) -> "\"" ++ toString (P.fromAbs item) ++ "\""
|
||||||
items -> (show . length $ items) ++ " items"
|
items -> (show . length $ items) ++ " items"
|
||||||
|
|
||||||
|
|
||||||
|
-- |Create a new file.
|
||||||
|
newFile :: MyGUI -> MyView -> IO ()
|
||||||
|
newFile _ myview = withErrorDialog $ do
|
||||||
|
mfn <- textInputDialog "Enter file name" ("" :: String)
|
||||||
|
let pmfn = P.parseRel =<< fromString <$> mfn
|
||||||
|
for_ pmfn $ \fn -> do
|
||||||
|
cdir <- getCurrentDir myview
|
||||||
|
createRegularFile newFilePerms (path cdir P.</> fn)
|
||||||
|
|
||||||
|
|
||||||
|
-- |Create a new directory.
|
||||||
|
newDir :: MyGUI -> MyView -> IO ()
|
||||||
|
newDir _ myview = withErrorDialog $ do
|
||||||
|
mfn <- textInputDialog "Enter directory name" ("" :: String)
|
||||||
|
let pmfn = P.parseRel =<< fromString <$> mfn
|
||||||
|
for_ pmfn $ \fn -> do
|
||||||
|
cdir <- getCurrentDir myview
|
||||||
|
createDir newDirPerms (path cdir P.</> fn)
|
||||||
|
|
||||||
|
|
||||||
|
renameF :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
|
renameF [item] _ _ = withErrorDialog $ do
|
||||||
|
iname <- P.fromRel <$> (P.basename $ path item)
|
||||||
|
mfn <- textInputDialog "Enter new file name" (iname :: ByteString)
|
||||||
|
let pmfn = P.parseRel =<< fromString <$> mfn
|
||||||
|
for_ pmfn $ \fn -> do
|
||||||
|
let cmsg = "Really rename \"" ++ getFPasStr item
|
||||||
|
++ "\"" ++ " to \""
|
||||||
|
++ toString (P.fromAbs $ (P.dirname . path $ item)
|
||||||
|
P.</> fn) ++ "\"?"
|
||||||
|
withConfirmationDialog cmsg $
|
||||||
|
HPath.IO.renameFile (path item)
|
||||||
|
((P.dirname $ path item) P.</> fn)
|
||||||
|
renameF _ _ _ = withErrorDialog
|
||||||
|
. ioError $ userError
|
||||||
|
"Operation not supported on multiple files"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- DIRECTORY TRAVERSAL AND FILE OPENING CALLBACKS ----
|
||||||
|
|
||||||
|
|
||||||
|
-- |Go to the url given at the 'urlBar' and visualize it in the given
|
||||||
|
-- treeView.
|
||||||
|
--
|
||||||
|
-- If the url is invalid, does nothing.
|
||||||
|
urlGoTo :: MyGUI -> MyView -> IO ()
|
||||||
|
urlGoTo mygui myview = withErrorDialog $ do
|
||||||
|
fp <- entryGetText (urlBar myview)
|
||||||
|
forM_ (P.parseAbs fp :: Maybe (Path Abs)) $ \fp' ->
|
||||||
|
whenM (canOpenDirectory fp')
|
||||||
|
(goDir True mygui myview =<< (pathToFile getFileInfo $ fp'))
|
||||||
|
|
||||||
|
|
||||||
|
goHome :: MyGUI -> MyView -> IO ()
|
||||||
|
goHome mygui myview = withErrorDialog $ do
|
||||||
|
homedir <- home
|
||||||
|
forM_ (P.parseAbs homedir :: Maybe (Path Abs)) $ \fp' ->
|
||||||
|
whenM (canOpenDirectory fp')
|
||||||
|
(goDir True mygui myview =<< (pathToFile getFileInfo $ fp'))
|
||||||
|
|
||||||
|
|
||||||
|
-- |Execute a given file.
|
||||||
|
execute :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
|
execute [item] _ _ = withErrorDialog $
|
||||||
|
void $ executeFile (path item) []
|
||||||
|
execute _ _ _ = withErrorDialog
|
||||||
|
. ioError $ userError
|
||||||
|
"Operation not supported on multiple files"
|
||||||
|
|
||||||
|
|
||||||
|
-- |Supposed to be used with 'withRows'. Opens a file or directory.
|
||||||
|
open :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
|
open [item] mygui myview = withErrorDialog $
|
||||||
|
case item of
|
||||||
|
DirOrSym r -> do
|
||||||
|
nv <- pathToFile getFileInfo $ path r
|
||||||
|
goDir True mygui myview nv
|
||||||
|
r ->
|
||||||
|
void $ openFile . path $ r
|
||||||
|
open items mygui myview = do
|
||||||
|
let dirs = filter (fst . sdir) items
|
||||||
|
files = filter (fst . sfileLike) items
|
||||||
|
forM_ dirs (withErrorDialog . opeInNewTab mygui myview)
|
||||||
|
forM_ files (withErrorDialog . openFile . path)
|
||||||
|
|
||||||
|
|
||||||
-- |Go up one directory and visualize it in the treeView.
|
-- |Go up one directory and visualize it in the treeView.
|
||||||
upDir :: MyGUI -> MyView -> IO ()
|
upDir :: MyGUI -> MyView -> IO ()
|
||||||
upDir mygui myview = withErrorDialog $ do
|
upDir mygui myview = withErrorDialog $ do
|
||||||
cdir <- getCurrentDir myview
|
cdir <- getCurrentDir myview
|
||||||
nv <- goUp cdir
|
nv <- goUp cdir
|
||||||
refreshView' mygui myview nv
|
goDir True mygui myview nv
|
||||||
|
|
||||||
|
|
||||||
-- |Go up one directory and visualize it in the treeView.
|
|
||||||
newFile :: MyGUI -> MyView -> IO ()
|
|
||||||
newFile _ myview = withErrorDialog $ do
|
|
||||||
mfn <- textInputDialog "Enter file name"
|
|
||||||
let pmfn = P.parseFn =<< P.userStringToFP <$> mfn
|
|
||||||
for_ pmfn $ \fn -> do
|
|
||||||
cdir <- getCurrentDir myview
|
|
||||||
createFile cdir fn
|
|
||||||
|
|
||||||
|
|
||||||
renameF :: [Item] -> MyGUI -> MyView -> IO ()
|
---- HISTORY CALLBACKS ----
|
||||||
renameF [item] _ _ = withErrorDialog $ do
|
|
||||||
mfn <- textInputDialog "Enter new file name"
|
|
||||||
let pmfn = P.parseFn =<< P.userStringToFP <$> mfn
|
-- |Go "back" in the history.
|
||||||
for_ pmfn $ \fn -> do
|
goHistoryBack :: MyGUI -> MyView -> IO (Path Abs)
|
||||||
let cmsg = "Really rename \"" ++ getFPasStr item
|
goHistoryBack mygui myview = do
|
||||||
++ "\"" ++ " to \""
|
hs <- takeMVar (history myview)
|
||||||
++ P.fpToString (P.fromAbs $ (P.dirname . path $ item)
|
let nhs = historyBack hs
|
||||||
P.</> fn) ++ "\"?"
|
putMVar (history myview) nhs
|
||||||
withConfirmationDialog cmsg $
|
nv <- pathToFile getFileInfo $ currentDir nhs
|
||||||
HSFM.FileSystem.FileOperations.renameFile item fn
|
goDir False mygui myview nv
|
||||||
renameF _ _ _ = withErrorDialog
|
return $ currentDir nhs
|
||||||
. throw $ InvalidOperation
|
|
||||||
"Operation not supported on multiple files"
|
|
||||||
|
-- |Go "forward" in the history.
|
||||||
|
goHistoryForward :: MyGUI -> MyView -> IO (Path Abs)
|
||||||
|
goHistoryForward mygui myview = do
|
||||||
|
hs <- takeMVar (history myview)
|
||||||
|
let nhs = historyForward hs
|
||||||
|
putMVar (history myview) nhs
|
||||||
|
nv <- pathToFile getFileInfo $ currentDir nhs
|
||||||
|
goDir False mygui myview nv
|
||||||
|
return $ currentDir nhs
|
||||||
|
|
||||||
|
|
||||||
|
-- |Show backwards history in a drop-down menu, depending on the input.
|
||||||
|
mkHistoryMenuB :: MyGUI -> MyView -> [Path Abs] -> IO Menu
|
||||||
|
mkHistoryMenuB mygui myview hs = do
|
||||||
|
menu <- menuNew
|
||||||
|
menuitems <- forM hs $ \p -> do
|
||||||
|
item <- menuItemNewWithLabel (fromAbs p)
|
||||||
|
_ <- item `on` menuItemActivated $
|
||||||
|
void $ iterateUntil (== p) (goHistoryBack mygui myview)
|
||||||
|
return item
|
||||||
|
forM_ menuitems $ \item -> menuShellAppend menu item
|
||||||
|
widgetShowAll menu
|
||||||
|
return menu
|
||||||
|
|
||||||
|
|
||||||
|
-- |Show forward history in a drop-down menu, depending on the input.
|
||||||
|
mkHistoryMenuF :: MyGUI -> MyView -> [Path Abs] -> IO Menu
|
||||||
|
mkHistoryMenuF mygui myview hs = do
|
||||||
|
menu <- menuNew
|
||||||
|
menuitems <- forM hs $ \p -> do
|
||||||
|
item <- menuItemNewWithLabel (fromAbs p)
|
||||||
|
_ <- item `on` menuItemActivated $
|
||||||
|
void $ iterateUntil (== p) (goHistoryForward mygui myview)
|
||||||
|
return item
|
||||||
|
forM_ menuitems $ \item -> menuShellAppend menu item
|
||||||
|
widgetShowAll menu
|
||||||
|
return menu
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- RIGHTCLICK CALLBACKS ----
|
||||||
|
|
||||||
|
|
||||||
|
-- |TODO: hopefully this does not leak
|
||||||
|
showPopup :: MyGUI -> MyView -> TimeStamp -> IO ()
|
||||||
|
showPopup mygui myview t
|
||||||
|
| null myplugins = return ()
|
||||||
|
| otherwise = do
|
||||||
|
|
||||||
|
rcmenu <- doRcMenu
|
||||||
|
|
||||||
|
-- add common callbacks
|
||||||
|
_ <- (\_ -> rcFileOpen rcmenu) myview `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview open
|
||||||
|
_ <- (rcFileExecute rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview execute
|
||||||
|
_ <- (rcFileNewRegFile rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ newFile mygui myview
|
||||||
|
_ <- (rcFileNewDir rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ newDir mygui myview
|
||||||
|
_ <- (rcFileNewTab rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ newTab' mygui myview
|
||||||
|
_ <- (rcFileNewTerm rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ void $ openTerminalHere myview
|
||||||
|
_ <- (rcFileCopy rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview copyInit
|
||||||
|
_ <- (rcFileRename rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview renameF
|
||||||
|
_ <- (rcFilePaste rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ operationFinal mygui myview Nothing
|
||||||
|
_ <- (rcFileDelete rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview del
|
||||||
|
_ <- (rcFileProperty rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview showFilePropertyDialog
|
||||||
|
_ <- (rcFileCut rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ withItems mygui myview moveInit
|
||||||
|
_ <- (rcFileIconView rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ switchView mygui myview createIconView
|
||||||
|
_ <- (rcFileTreeView rcmenu) `on` menuItemActivated $
|
||||||
|
liftIO $ switchView mygui myview createTreeView
|
||||||
|
|
||||||
|
|
||||||
|
-- add another plugin separator after the existing one
|
||||||
|
-- where we want to place our plugins
|
||||||
|
sep2 <- separatorMenuItemNew
|
||||||
|
widgetShow sep2
|
||||||
|
|
||||||
|
menuShellInsert (rcMenu rcmenu) sep2 insertPos
|
||||||
|
|
||||||
|
plugins <- forM myplugins $ \(ma, mb, mc) -> fmap (, mb, mc) ma
|
||||||
|
-- need to reverse plugins list so the order is right
|
||||||
|
forM_ (reverse plugins) $ \(plugin, filter', cb) -> do
|
||||||
|
showItem <- withItems mygui myview filter'
|
||||||
|
|
||||||
|
menuShellInsert (rcMenu rcmenu) plugin insertPos
|
||||||
|
when showItem $ widgetShow plugin
|
||||||
|
-- init callback
|
||||||
|
plugin `on` menuItemActivated $ withItems mygui myview cb
|
||||||
|
|
||||||
|
menuPopup (rcMenu rcmenu) $ Just (RightButton, t)
|
||||||
|
where
|
||||||
|
doRcMenu = do
|
||||||
|
builder <- builderNew
|
||||||
|
builderAddFromFile builder =<< getDataFileName "data/Gtk/builder.xml"
|
||||||
|
|
||||||
|
-- create static right-click menu
|
||||||
|
rcMenu <- builderGetObject builder castToMenu
|
||||||
|
(fromString "rcMenu")
|
||||||
|
rcFileOpen <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileOpen")
|
||||||
|
rcFileExecute <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileExecute")
|
||||||
|
rcFileNewRegFile <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileNewRegFile")
|
||||||
|
rcFileNewDir <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileNewDir")
|
||||||
|
rcFileNewTab <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileNewTab")
|
||||||
|
rcFileNewTerm <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileNewTerm")
|
||||||
|
rcFileCut <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileCut")
|
||||||
|
rcFileCopy <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileCopy")
|
||||||
|
rcFileRename <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileRename")
|
||||||
|
rcFilePaste <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFilePaste")
|
||||||
|
rcFileDelete <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileDelete")
|
||||||
|
rcFileProperty <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileProperty")
|
||||||
|
rcFileIconView <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileIconView")
|
||||||
|
rcFileTreeView <- builderGetObject builder castToImageMenuItem
|
||||||
|
(fromString "rcFileTreeView")
|
||||||
|
|
||||||
|
return $ MkRightClickMenu {..}
|
||||||
|
|
||||||
|
|||||||
@@ -22,4 +22,4 @@ module HSFM.GUI.Gtk.Callbacks where
|
|||||||
import HSFM.GUI.Gtk.Data
|
import HSFM.GUI.Gtk.Data
|
||||||
|
|
||||||
|
|
||||||
setCallbacks :: MyGUI -> MyView -> IO ()
|
setViewCallbacks :: MyGUI -> MyView -> IO ()
|
||||||
|
|||||||
128
src/HSFM/GUI/Gtk/Callbacks/Utils.hs
Normal file
128
src/HSFM/GUI/Gtk/Callbacks/Utils.hs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-# LANGUAGE ScopedTypeVariables #-}
|
||||||
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
|
module HSFM.GUI.Gtk.Callbacks.Utils where
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
import Control.Monad
|
||||||
|
(
|
||||||
|
forM_
|
||||||
|
, when
|
||||||
|
)
|
||||||
|
import Data.Foldable
|
||||||
|
(
|
||||||
|
for_
|
||||||
|
)
|
||||||
|
import Data.Maybe
|
||||||
|
(
|
||||||
|
fromJust
|
||||||
|
)
|
||||||
|
import GHC.IO.Exception
|
||||||
|
(
|
||||||
|
IOErrorType(..)
|
||||||
|
)
|
||||||
|
import Graphics.UI.Gtk
|
||||||
|
import qualified HPath as P
|
||||||
|
import HPath.IO
|
||||||
|
import HPath.IO.Errors
|
||||||
|
import HSFM.FileSystem.FileType
|
||||||
|
import qualified HSFM.FileSystem.UtilTypes as UT
|
||||||
|
import HSFM.GUI.Gtk.Data
|
||||||
|
import HSFM.GUI.Gtk.Dialogs
|
||||||
|
import HSFM.GUI.Gtk.MyView
|
||||||
|
import HSFM.History
|
||||||
|
import Prelude hiding(readFile)
|
||||||
|
import Control.Concurrent.MVar
|
||||||
|
(
|
||||||
|
putMVar
|
||||||
|
, tryTakeMVar
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- |Carries out a file operation with the appropriate error handling
|
||||||
|
-- allowing the user to react to various exceptions with further input.
|
||||||
|
doFileOperation :: UT.FileOperation -> IO ()
|
||||||
|
doFileOperation (UT.FCopy (UT.Copy (f':fs') to)) =
|
||||||
|
_doFileOperation (f':fs') to (\p1 p2 cm -> easyCopy p1 p2 cm FailEarly)
|
||||||
|
$ doFileOperation (UT.FCopy $ UT.Copy fs' to)
|
||||||
|
doFileOperation (UT.FMove (UT.Move (f':fs') to)) =
|
||||||
|
_doFileOperation (f':fs') to moveFile
|
||||||
|
$ doFileOperation (UT.FMove $ UT.Move fs' to)
|
||||||
|
doFileOperation _ = return ()
|
||||||
|
|
||||||
|
|
||||||
|
_doFileOperation :: [P.Path b1]
|
||||||
|
-> P.Path P.Abs
|
||||||
|
-> (P.Path b1 -> P.Path P.Abs -> CopyMode -> IO b)
|
||||||
|
-> IO ()
|
||||||
|
-> IO ()
|
||||||
|
_doFileOperation [] _ _ _ = return ()
|
||||||
|
_doFileOperation (f:fs) to mc rest = do
|
||||||
|
toname <- P.basename f
|
||||||
|
let topath = to P.</> toname
|
||||||
|
reactOnError (mc f topath Strict >> rest)
|
||||||
|
-- TODO: how safe is 'AlreadyExists' here?
|
||||||
|
[(AlreadyExists , collisionAction fileCollisionDialog topath)]
|
||||||
|
[(SameFile{} , collisionAction renameDialog topath)]
|
||||||
|
where
|
||||||
|
collisionAction diag topath = do
|
||||||
|
mcm <- diag . P.fromAbs $ topath
|
||||||
|
forM_ mcm $ \cm -> case cm of
|
||||||
|
UT.Overwrite -> mc f topath Overwrite >> rest
|
||||||
|
UT.OverwriteAll -> forM_ (f:fs) $ \x -> do
|
||||||
|
toname' <- P.basename x
|
||||||
|
mc x (to P.</> toname') Overwrite
|
||||||
|
UT.Skip -> rest
|
||||||
|
UT.Rename newn -> mc f (to P.</> newn) Strict >> rest
|
||||||
|
_ -> return ()
|
||||||
|
|
||||||
|
|
||||||
|
-- |Helper that is invoked for any directory change operations.
|
||||||
|
goDir :: Bool -- ^ whether to update the history
|
||||||
|
-> MyGUI
|
||||||
|
-> MyView
|
||||||
|
-> Item
|
||||||
|
-> IO ()
|
||||||
|
goDir bhis mygui myview item = do
|
||||||
|
when bhis $ do
|
||||||
|
mhs <- tryTakeMVar (history myview)
|
||||||
|
for_ mhs $ \hs -> do
|
||||||
|
let nhs = historyNewPath (path item) hs
|
||||||
|
putMVar (history myview) nhs
|
||||||
|
refreshView mygui myview item
|
||||||
|
|
||||||
|
-- set notebook tab label
|
||||||
|
page <- notebookGetCurrentPage (notebook myview)
|
||||||
|
child <- fromJust <$> notebookGetNthPage (notebook myview) page
|
||||||
|
|
||||||
|
-- get the label
|
||||||
|
ebox <- (castToEventBox . fromJust)
|
||||||
|
<$> notebookGetTabLabel (notebook myview) child
|
||||||
|
label <- (castToLabel . head) <$> containerGetChildren ebox
|
||||||
|
|
||||||
|
-- set the label
|
||||||
|
labelSetText label
|
||||||
|
(maybe (P.fromAbs $ path item)
|
||||||
|
P.fromRel $ P.basename . path $ item)
|
||||||
|
|
||||||
@@ -29,10 +29,11 @@ import Control.Concurrent.STM
|
|||||||
(
|
(
|
||||||
TVar
|
TVar
|
||||||
)
|
)
|
||||||
import Graphics.UI.Gtk
|
import Graphics.UI.Gtk hiding (MenuBar)
|
||||||
import HSFM.FileSystem.FileOperations
|
|
||||||
import HSFM.FileSystem.FileType
|
import HSFM.FileSystem.FileType
|
||||||
import System.INotify.ByteString
|
import HSFM.FileSystem.UtilTypes
|
||||||
|
import HSFM.History
|
||||||
|
import System.INotify
|
||||||
(
|
(
|
||||||
INotify
|
INotify
|
||||||
)
|
)
|
||||||
@@ -50,65 +51,107 @@ import System.INotify.ByteString
|
|||||||
-- runtime.
|
-- runtime.
|
||||||
data MyGUI = MkMyGUI {
|
data MyGUI = MkMyGUI {
|
||||||
-- |main Window
|
-- |main Window
|
||||||
rootWin :: Window
|
rootWin :: !Window
|
||||||
, menubarFileQuit :: ImageMenuItem
|
|
||||||
, menubarFileOpen :: ImageMenuItem
|
-- widgets on the main window
|
||||||
, menubarFileExecute :: ImageMenuItem
|
, menubar :: !MenuBar
|
||||||
, menubarFileNew :: ImageMenuItem
|
, statusBar :: !Statusbar
|
||||||
, menubarEditCut :: ImageMenuItem
|
, clearStatusBar :: !Button
|
||||||
, menubarEditCopy :: ImageMenuItem
|
|
||||||
, menubarEditRename :: ImageMenuItem
|
, notebook1 :: !Notebook
|
||||||
, menubarEditPaste :: ImageMenuItem
|
, leftNbBtn :: !ToggleButton
|
||||||
, menubarEditDelete :: ImageMenuItem
|
, leftNbIcon :: !Image
|
||||||
, menubarViewTree :: ImageMenuItem
|
|
||||||
, menubarViewIcon :: ImageMenuItem
|
, notebook2 :: !Notebook
|
||||||
, menubarHelpAbout :: ImageMenuItem
|
, rightNbBtn :: !ToggleButton
|
||||||
, rcMenu :: Menu
|
, rightNbIcon :: !Image
|
||||||
, rcFileOpen :: ImageMenuItem
|
|
||||||
, rcFileExecute :: ImageMenuItem
|
-- other
|
||||||
, rcFileNew :: ImageMenuItem
|
, fprop :: !FilePropertyGrid
|
||||||
, rcFileCut :: ImageMenuItem
|
, settings :: !(TVar FMSettings)
|
||||||
, rcFileCopy :: ImageMenuItem
|
|
||||||
, rcFileRename :: ImageMenuItem
|
, operationBuffer :: !(TVar FileOperation)
|
||||||
, rcFilePaste :: ImageMenuItem
|
}
|
||||||
, rcFileDelete :: ImageMenuItem
|
|
||||||
, upViewB :: Button
|
|
||||||
, homeViewB :: Button
|
-- |This describes the contents of the current view and is separated from MyGUI,
|
||||||
, refreshViewB :: Button
|
-- because we might want to have multiple views.
|
||||||
, urlBar :: Entry
|
data MyView = MkMyView {
|
||||||
, statusBar :: Statusbar
|
view :: !(TVar FMView)
|
||||||
, clearStatusBar :: Button
|
, cwd :: !(MVar Item)
|
||||||
, settings :: TVar FMSettings
|
, rawModel :: !(TVar (ListStore Item))
|
||||||
, scroll :: ScrolledWindow
|
, sortedModel :: !(TVar (TypedTreeModelSort Item))
|
||||||
|
, filteredModel :: !(TVar (TypedTreeModelFilter Item))
|
||||||
|
, inotify :: !(MVar INotify)
|
||||||
|
, notebook :: !Notebook -- current notebook
|
||||||
|
|
||||||
|
-- the first part of the tuple represents the "go back"
|
||||||
|
-- the second part the "go forth" in the history
|
||||||
|
, history :: !(MVar BrowsingHistory)
|
||||||
|
|
||||||
|
-- sub-widgets
|
||||||
|
, scroll :: !ScrolledWindow
|
||||||
|
, viewBox :: !Box
|
||||||
|
, backViewB :: !Button
|
||||||
|
, upViewB :: !Button
|
||||||
|
, forwardViewB :: !Button
|
||||||
|
, homeViewB :: !Button
|
||||||
|
, refreshViewB :: !Button
|
||||||
|
, urlBar :: !Entry
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
data MenuBar = MkMenuBar {
|
||||||
|
menubarFileQuit :: !ImageMenuItem
|
||||||
|
, menubarHelpAbout :: !ImageMenuItem
|
||||||
|
}
|
||||||
|
|
||||||
|
data RightClickMenu = MkRightClickMenu {
|
||||||
|
rcMenu :: !Menu
|
||||||
|
, rcFileOpen :: !ImageMenuItem
|
||||||
|
, rcFileExecute :: !ImageMenuItem
|
||||||
|
, rcFileNewRegFile :: !ImageMenuItem
|
||||||
|
, rcFileNewDir :: !ImageMenuItem
|
||||||
|
, rcFileNewTab :: !ImageMenuItem
|
||||||
|
, rcFileNewTerm :: !ImageMenuItem
|
||||||
|
, rcFileCut :: !ImageMenuItem
|
||||||
|
, rcFileCopy :: !ImageMenuItem
|
||||||
|
, rcFileRename :: !ImageMenuItem
|
||||||
|
, rcFilePaste :: !ImageMenuItem
|
||||||
|
, rcFileDelete :: !ImageMenuItem
|
||||||
|
, rcFileProperty :: !ImageMenuItem
|
||||||
|
, rcFileIconView :: !ImageMenuItem
|
||||||
|
, rcFileTreeView :: !ImageMenuItem
|
||||||
|
}
|
||||||
|
|
||||||
|
data FilePropertyGrid = MkFilePropertyGrid {
|
||||||
|
fpropGrid :: !Grid
|
||||||
|
, fpropFnEntry :: !Entry
|
||||||
|
, fpropLocEntry :: !Entry
|
||||||
|
, fpropTsEntry :: !Entry
|
||||||
|
, fpropModEntry :: !Entry
|
||||||
|
, fpropAcEntry :: !Entry
|
||||||
|
, fpropFTEntry :: !Entry
|
||||||
|
, fpropPermEntry :: !Entry
|
||||||
|
, fpropLDEntry :: !Entry
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
-- |FM-wide settings.
|
-- |FM-wide settings.
|
||||||
data FMSettings = MkFMSettings {
|
data FMSettings = MkFMSettings {
|
||||||
showHidden :: Bool
|
showHidden :: !Bool
|
||||||
, isLazy :: Bool
|
, isLazy :: !Bool
|
||||||
, iconSize :: Int
|
, iconSize :: !Int
|
||||||
}
|
}
|
||||||
|
|
||||||
data FMView = FMTreeView TreeView
|
data FMView = FMTreeView !TreeView
|
||||||
| FMIconView IconView
|
| FMIconView !IconView
|
||||||
|
|
||||||
type Item = File FileInfo
|
type Item = File FileInfo
|
||||||
|
|
||||||
|
|
||||||
-- |This describes the contents of the current vie and is separated from MyGUI,
|
|
||||||
-- because we might want to have multiple views.
|
|
||||||
data MyView = MkMyView {
|
|
||||||
view :: TVar FMView
|
|
||||||
, cwd :: MVar Item
|
|
||||||
, rawModel :: TVar (ListStore Item)
|
|
||||||
, sortedModel :: TVar (TypedTreeModelSort Item)
|
|
||||||
, filteredModel :: TVar (TypedTreeModelFilter Item)
|
|
||||||
, operationBuffer :: TVar FileOperation
|
|
||||||
, inotify :: MVar INotify
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fmViewToContainer :: FMView -> Container
|
fmViewToContainer :: FMView -> Container
|
||||||
fmViewToContainer (FMTreeView x) = castToContainer . toGObject $ x
|
fmViewToContainer (FMTreeView x) = castToContainer . toGObject $ x
|
||||||
fmViewToContainer (FMIconView x) = castToContainer . toGObject $ x
|
fmViewToContainer (FMIconView x) = castToContainer . toGObject $ x
|
||||||
|
|
||||||
|
|||||||
@@ -16,18 +16,22 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
|
{-# LANGUAGE CPP #-}
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
module HSFM.GUI.Gtk.Dialogs where
|
module HSFM.GUI.Gtk.Dialogs where
|
||||||
|
|
||||||
|
|
||||||
|
import Codec.Binary.UTF8.String
|
||||||
|
(
|
||||||
|
decodeString
|
||||||
|
)
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
(
|
(
|
||||||
catch
|
catches
|
||||||
, displayException
|
, displayException
|
||||||
, throw
|
, throwIO
|
||||||
, IOException
|
, IOException
|
||||||
, catches
|
|
||||||
, Handler(..)
|
, Handler(..)
|
||||||
)
|
)
|
||||||
import Control.Monad
|
import Control.Monad
|
||||||
@@ -36,23 +40,48 @@ import Control.Monad
|
|||||||
, when
|
, when
|
||||||
, void
|
, void
|
||||||
)
|
)
|
||||||
import Data.Version
|
import Data.ByteString
|
||||||
(
|
(
|
||||||
showVersion
|
ByteString
|
||||||
|
)
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
import Data.ByteString.UTF8
|
||||||
|
(
|
||||||
|
fromString
|
||||||
)
|
)
|
||||||
import Distribution.Package
|
import Distribution.Package
|
||||||
(
|
(
|
||||||
PackageIdentifier(..)
|
PackageIdentifier(..)
|
||||||
, PackageName(..)
|
, packageVersion
|
||||||
|
, unPackageName
|
||||||
)
|
)
|
||||||
|
#if MIN_VERSION_Cabal(2,0,0)
|
||||||
|
import Distribution.Version
|
||||||
|
(
|
||||||
|
showVersion
|
||||||
|
)
|
||||||
|
#else
|
||||||
|
import Data.Version
|
||||||
|
(
|
||||||
|
showVersion
|
||||||
|
)
|
||||||
|
#endif
|
||||||
import Distribution.PackageDescription
|
import Distribution.PackageDescription
|
||||||
(
|
(
|
||||||
GenericPackageDescription(..)
|
GenericPackageDescription(..)
|
||||||
, PackageDescription(..)
|
, PackageDescription(..)
|
||||||
)
|
)
|
||||||
|
#if MIN_VERSION_Cabal(2,2,0)
|
||||||
|
import Distribution.PackageDescription.Parsec
|
||||||
|
#else
|
||||||
import Distribution.PackageDescription.Parse
|
import Distribution.PackageDescription.Parse
|
||||||
|
#endif
|
||||||
(
|
(
|
||||||
readPackageDescription
|
#if MIN_VERSION_Cabal(2,0,0)
|
||||||
|
readGenericPackageDescription,
|
||||||
|
#else
|
||||||
|
readPackageDescription,
|
||||||
|
#endif
|
||||||
)
|
)
|
||||||
import Distribution.Verbosity
|
import Distribution.Verbosity
|
||||||
(
|
(
|
||||||
@@ -60,13 +89,27 @@ import Distribution.Verbosity
|
|||||||
)
|
)
|
||||||
import Graphics.UI.Gtk
|
import Graphics.UI.Gtk
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
import HSFM.FileSystem.Errors
|
import HPath.IO.Errors
|
||||||
import HSFM.FileSystem.FileOperations
|
import HSFM.FileSystem.FileType
|
||||||
|
import HSFM.FileSystem.UtilTypes
|
||||||
|
import HSFM.GUI.Glib.GlibString()
|
||||||
|
import HSFM.GUI.Gtk.Data
|
||||||
import HSFM.GUI.Gtk.Errors
|
import HSFM.GUI.Gtk.Errors
|
||||||
import Paths_hsfm
|
import Paths_hsfm
|
||||||
(
|
(
|
||||||
getDataFileName
|
getDataFileName
|
||||||
)
|
)
|
||||||
|
import System.Glib.UTFString
|
||||||
|
(
|
||||||
|
GlibString
|
||||||
|
)
|
||||||
|
import System.Posix.FilePath
|
||||||
|
(
|
||||||
|
takeFileName
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -104,91 +147,77 @@ showConfirmationDialog str = do
|
|||||||
_ -> return False
|
_ -> return False
|
||||||
|
|
||||||
|
|
||||||
-- |Asks the user which directory copy mode he wants via dialog popup
|
fileCollisionDialog :: ByteString -> IO (Maybe FCollisonMode)
|
||||||
-- and returns 'DirCopyMode'. Default is always Strict, so this allows
|
fileCollisionDialog t = do
|
||||||
-- switching to Merge/Replace/Rename.
|
|
||||||
showCopyModeDialog :: IO (Maybe CopyMode)
|
|
||||||
showCopyModeDialog = do
|
|
||||||
chooserDialog <- messageDialogNew Nothing
|
chooserDialog <- messageDialogNew Nothing
|
||||||
[DialogDestroyWithParent]
|
[DialogDestroyWithParent]
|
||||||
MessageQuestion
|
MessageQuestion
|
||||||
ButtonsNone
|
ButtonsNone
|
||||||
"Target exists, how to proceed?"
|
(fromString "Target \"" `BS.append`
|
||||||
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 0)
|
t `BS.append`
|
||||||
_ <- dialogAddButton chooserDialog "Merge" (ResponseUser 1)
|
fromString "\" exists, how to proceed?")
|
||||||
_ <- dialogAddButton chooserDialog "Replace" (ResponseUser 2)
|
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 0)
|
||||||
_ <- dialogAddButton chooserDialog "Rename" (ResponseUser 3)
|
_ <- dialogAddButton chooserDialog "Overwrite" (ResponseUser 1)
|
||||||
|
_ <- dialogAddButton chooserDialog "Overwrite all" (ResponseUser 2)
|
||||||
|
_ <- dialogAddButton chooserDialog "Skip" (ResponseUser 3)
|
||||||
|
_ <- dialogAddButton chooserDialog "Rename" (ResponseUser 4)
|
||||||
rID <- dialogRun chooserDialog
|
rID <- dialogRun chooserDialog
|
||||||
widgetDestroy chooserDialog
|
widgetDestroy chooserDialog
|
||||||
case rID of
|
case rID of
|
||||||
ResponseUser 0 -> return Nothing
|
ResponseUser 0 -> return Nothing
|
||||||
ResponseUser 1 -> return (Just Merge)
|
ResponseUser 1 -> return (Just Overwrite)
|
||||||
ResponseUser 2 -> return (Just Replace)
|
ResponseUser 2 -> return (Just OverwriteAll)
|
||||||
ResponseUser 3 -> do
|
ResponseUser 3 -> return (Just Skip)
|
||||||
mfn <- textInputDialog "Enter new name"
|
ResponseUser 4 -> do
|
||||||
|
mfn <- textInputDialog (fromString "Enter new name") (takeFileName t)
|
||||||
forM mfn $ \fn -> do
|
forM mfn $ \fn -> do
|
||||||
pfn <- P.parseFn (P.userStringToFP fn)
|
pfn <- P.parseRel (fromString fn)
|
||||||
return $ Rename pfn
|
return $ Rename pfn
|
||||||
_ -> throw UnknownDialogButton
|
_ -> throwIO UnknownDialogButton
|
||||||
|
|
||||||
|
|
||||||
-- |Stipped version of `showCopyModeDialog` that only allows cancelling
|
renameDialog :: ByteString -> IO (Maybe FCollisonMode)
|
||||||
-- or Renaming.
|
renameDialog t = do
|
||||||
showRenameDialog :: IO (Maybe CopyMode)
|
|
||||||
showRenameDialog = do
|
|
||||||
chooserDialog <- messageDialogNew Nothing
|
chooserDialog <- messageDialogNew Nothing
|
||||||
[DialogDestroyWithParent]
|
[DialogDestroyWithParent]
|
||||||
MessageQuestion
|
MessageQuestion
|
||||||
ButtonsNone
|
ButtonsNone
|
||||||
"Target exists, how to proceed?"
|
(fromString "Target \"" `BS.append`
|
||||||
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 0)
|
t `BS.append`
|
||||||
_ <- dialogAddButton chooserDialog "Rename" (ResponseUser 1)
|
fromString "\" exists, how to proceed?")
|
||||||
|
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 0)
|
||||||
|
_ <- dialogAddButton chooserDialog "Skip" (ResponseUser 1)
|
||||||
|
_ <- dialogAddButton chooserDialog "Rename" (ResponseUser 2)
|
||||||
rID <- dialogRun chooserDialog
|
rID <- dialogRun chooserDialog
|
||||||
widgetDestroy chooserDialog
|
widgetDestroy chooserDialog
|
||||||
case rID of
|
case rID of
|
||||||
ResponseUser 0 -> return Nothing
|
ResponseUser 0 -> return Nothing
|
||||||
ResponseUser 1 -> do
|
ResponseUser 1 -> return (Just Skip)
|
||||||
mfn <- textInputDialog "Enter new name"
|
ResponseUser 2 -> do
|
||||||
|
mfn <- textInputDialog (fromString "Enter new name") (takeFileName t)
|
||||||
forM mfn $ \fn -> do
|
forM mfn $ \fn -> do
|
||||||
pfn <- P.parseFn (P.userStringToFP fn)
|
pfn <- P.parseRel (fromString fn)
|
||||||
return $ Rename pfn
|
return $ Rename pfn
|
||||||
_ -> throw UnknownDialogButton
|
_ -> throwIO UnknownDialogButton
|
||||||
|
|
||||||
|
|
||||||
-- |Attempts to run the given function with the `Strict` copy mode.
|
|
||||||
-- If that raises a `FileDoesExist` or `DirDoesExist`, then it prompts
|
|
||||||
-- the user for action via `showCopyModeDialog` and then carries out
|
|
||||||
-- the given function again.
|
|
||||||
withCopyModeDialog :: (CopyMode -> IO ()) -> IO ()
|
|
||||||
withCopyModeDialog fa =
|
|
||||||
catch (fa Strict) $ \e ->
|
|
||||||
case e of
|
|
||||||
FileDoesExist _ -> doIt showCopyModeDialog
|
|
||||||
DirDoesExist _ -> doIt showCopyModeDialog
|
|
||||||
SameFile _ _ -> doIt showRenameDialog
|
|
||||||
e' -> throw e'
|
|
||||||
where
|
|
||||||
doIt getCm = do
|
|
||||||
mcm <- getCm
|
|
||||||
case mcm of
|
|
||||||
(Just Strict) -> return () -- don't try again
|
|
||||||
(Just cm) -> fa cm
|
|
||||||
Nothing -> return ()
|
|
||||||
|
|
||||||
|
|
||||||
-- |Shows the about dialog from the help menu.
|
-- |Shows the about dialog from the help menu.
|
||||||
showAboutDialog :: IO ()
|
showAboutDialog :: IO ()
|
||||||
showAboutDialog = do
|
showAboutDialog = do
|
||||||
ad <- aboutDialogNew
|
ad <- aboutDialogNew
|
||||||
lstr <- readFile =<< getDataFileName "LICENSE"
|
lstr <- Prelude.readFile =<< getDataFileName "LICENSE"
|
||||||
hsfmicon <- pixbufNewFromFile =<< getDataFileName "data/Gtk/icons/hsfm.png"
|
hsfmicon <- pixbufNewFromFile =<< getDataFileName "data/Gtk/icons/hsfm.png"
|
||||||
pdesc <- fmap packageDescription
|
pdesc <- fmap packageDescription
|
||||||
|
#if MIN_VERSION_Cabal(2,0,0)
|
||||||
|
(readGenericPackageDescription silent
|
||||||
|
#else
|
||||||
(readPackageDescription silent
|
(readPackageDescription silent
|
||||||
|
#endif
|
||||||
=<< getDataFileName "hsfm.cabal")
|
=<< getDataFileName "hsfm.cabal")
|
||||||
set ad
|
set ad
|
||||||
[ aboutDialogProgramName := (unPackageName . pkgName . package) pdesc
|
[ aboutDialogProgramName := (unPackageName . pkgName . package) pdesc
|
||||||
, aboutDialogName := (unPackageName . pkgName . package) pdesc
|
, aboutDialogName := (unPackageName . pkgName . package) pdesc
|
||||||
, aboutDialogVersion := (showVersion . pkgVersion . package) pdesc
|
, aboutDialogVersion := (showVersion . packageVersion . package) pdesc
|
||||||
, aboutDialogCopyright := copyright pdesc
|
, aboutDialogCopyright := copyright pdesc
|
||||||
, aboutDialogComments := description pdesc
|
, aboutDialogComments := description pdesc
|
||||||
, aboutDialogLicense := Just lstr
|
, aboutDialogLicense := Just lstr
|
||||||
@@ -215,22 +244,28 @@ withErrorDialog :: IO a -> IO ()
|
|||||||
withErrorDialog io =
|
withErrorDialog io =
|
||||||
catches (void io)
|
catches (void io)
|
||||||
[ Handler (\e -> showErrorDialog
|
[ Handler (\e -> showErrorDialog
|
||||||
$ displayException (e :: IOException))
|
. decodeString
|
||||||
|
. displayException
|
||||||
|
$ (e :: IOException))
|
||||||
, Handler (\e -> showErrorDialog
|
, Handler (\e -> showErrorDialog
|
||||||
$ displayException (e :: FmIOException))
|
$ displayException (e :: HPathIOException))
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
-- |Asks the user which directory copy mode he wants via dialog popup
|
-- |Asks the user which directory copy mode he wants via dialog popup
|
||||||
-- and returns 'DirCopyMode'.
|
-- and returns 'DirCopyMode'.
|
||||||
textInputDialog :: String -> IO (Maybe String)
|
textInputDialog :: (GlibString s1, GlibString s2)
|
||||||
textInputDialog title = do
|
=> s1 -- ^ window title
|
||||||
|
-> s2 -- ^ initial text in input widget
|
||||||
|
-> IO (Maybe String)
|
||||||
|
textInputDialog title inittext = do
|
||||||
chooserDialog <- messageDialogNew Nothing
|
chooserDialog <- messageDialogNew Nothing
|
||||||
[DialogDestroyWithParent]
|
[DialogDestroyWithParent]
|
||||||
MessageQuestion
|
MessageQuestion
|
||||||
ButtonsNone
|
ButtonsNone
|
||||||
title
|
title
|
||||||
entry <- entryNew
|
entry <- entryNew
|
||||||
|
entrySetText entry inittext
|
||||||
cbox <- dialogGetActionArea chooserDialog
|
cbox <- dialogGetActionArea chooserDialog
|
||||||
_ <- dialogAddButton chooserDialog "Ok" (ResponseUser 0)
|
_ <- dialogAddButton chooserDialog "Ok" (ResponseUser 0)
|
||||||
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 1)
|
_ <- dialogAddButton chooserDialog "Cancel" (ResponseUser 1)
|
||||||
@@ -241,6 +276,53 @@ textInputDialog title = do
|
|||||||
-- TODO: make this more safe
|
-- TODO: make this more safe
|
||||||
ResponseUser 0 -> Just <$> entryGetText entry
|
ResponseUser 0 -> Just <$> entryGetText entry
|
||||||
ResponseUser 1 -> return Nothing
|
ResponseUser 1 -> return Nothing
|
||||||
_ -> throw UnknownDialogButton
|
_ -> throwIO UnknownDialogButton
|
||||||
widgetDestroy chooserDialog
|
widgetDestroy chooserDialog
|
||||||
return ret
|
return ret
|
||||||
|
|
||||||
|
|
||||||
|
showFilePropertyDialog :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
|
showFilePropertyDialog [item] mygui _ = do
|
||||||
|
dialog <- messageDialogNew Nothing
|
||||||
|
[DialogDestroyWithParent]
|
||||||
|
MessageInfo
|
||||||
|
ButtonsNone
|
||||||
|
"File Properties"
|
||||||
|
|
||||||
|
let fprop' = fprop mygui
|
||||||
|
grid = fpropGrid fprop'
|
||||||
|
|
||||||
|
entrySetText (fpropFnEntry fprop') (maybe BS.empty P.fromRel
|
||||||
|
$ P.basename . path $ item)
|
||||||
|
entrySetText (fpropLocEntry fprop') (P.fromAbs . P.dirname . path $ item)
|
||||||
|
entrySetText (fpropTsEntry fprop') (show . fileSize $ fvar item)
|
||||||
|
entrySetText (fpropModEntry fprop') (packModTime item)
|
||||||
|
entrySetText (fpropAcEntry fprop') (packAccessTime item)
|
||||||
|
entrySetText (fpropFTEntry fprop') (packFileType item)
|
||||||
|
entrySetText (fpropPermEntry fprop')
|
||||||
|
(tail $ packPermissions item) -- throw away the filetype part
|
||||||
|
case packLinkDestination item of
|
||||||
|
(Just dest) -> do
|
||||||
|
widgetSetSensitive (fpropLDEntry fprop') True
|
||||||
|
entrySetText (fpropLDEntry fprop') dest
|
||||||
|
Nothing -> do
|
||||||
|
widgetSetSensitive (fpropLDEntry fprop') False
|
||||||
|
entrySetText (fpropLDEntry fprop') "( Not a symlink )"
|
||||||
|
|
||||||
|
|
||||||
|
cbox <- dialogGetActionArea dialog
|
||||||
|
_ <- dialogAddButton dialog "Ok" (ResponseUser 0)
|
||||||
|
_ <- dialogAddButton dialog "Cancel" (ResponseUser 1)
|
||||||
|
boxPackStart (castToBox cbox) grid PackNatural 5
|
||||||
|
|
||||||
|
widgetShowAll dialog
|
||||||
|
_ <- dialogRun dialog
|
||||||
|
|
||||||
|
-- make sure our grid does not get destroyed
|
||||||
|
containerRemove (castToBox cbox) grid
|
||||||
|
|
||||||
|
widgetDestroy dialog
|
||||||
|
|
||||||
|
return ()
|
||||||
|
showFilePropertyDialog _ _ _ = return ()
|
||||||
|
|
||||||
|
|||||||
@@ -16,6 +16,7 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
module HSFM.GUI.Gtk.MyGUI where
|
module HSFM.GUI.Gtk.MyGUI where
|
||||||
@@ -26,6 +27,7 @@ import Control.Concurrent.STM
|
|||||||
newTVarIO
|
newTVarIO
|
||||||
)
|
)
|
||||||
import Graphics.UI.Gtk
|
import Graphics.UI.Gtk
|
||||||
|
import HSFM.FileSystem.UtilTypes
|
||||||
import HSFM.GUI.Gtk.Data
|
import HSFM.GUI.Gtk.Data
|
||||||
import Paths_hsfm
|
import Paths_hsfm
|
||||||
(
|
(
|
||||||
@@ -43,9 +45,9 @@ import Paths_hsfm
|
|||||||
-- |Set up the GUI. This only creates the permanent widgets.
|
-- |Set up the GUI. This only creates the permanent widgets.
|
||||||
createMyGUI :: IO MyGUI
|
createMyGUI :: IO MyGUI
|
||||||
createMyGUI = do
|
createMyGUI = do
|
||||||
|
|
||||||
let settings' = MkFMSettings False True 24
|
let settings' = MkFMSettings False True 24
|
||||||
settings <- newTVarIO settings'
|
settings <- newTVarIO settings'
|
||||||
|
operationBuffer <- newTVarIO None
|
||||||
|
|
||||||
builder <- builderNew
|
builder <- builderNew
|
||||||
builderAddFromFile builder =<< getDataFileName "data/Gtk/builder.xml"
|
builderAddFromFile builder =<< getDataFileName "data/Gtk/builder.xml"
|
||||||
@@ -53,64 +55,62 @@ createMyGUI = do
|
|||||||
-- get the pre-defined gui widgets
|
-- get the pre-defined gui widgets
|
||||||
rootWin <- builderGetObject builder castToWindow
|
rootWin <- builderGetObject builder castToWindow
|
||||||
"rootWin"
|
"rootWin"
|
||||||
scroll <- builderGetObject builder castToScrolledWindow
|
|
||||||
"mainScroll"
|
|
||||||
menubarFileQuit <- builderGetObject builder castToImageMenuItem
|
menubarFileQuit <- builderGetObject builder castToImageMenuItem
|
||||||
"menubarFileQuit"
|
"menubarFileQuit"
|
||||||
menubarFileOpen <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarFileOpen"
|
|
||||||
menubarFileExecute <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarFileExecute"
|
|
||||||
menubarFileNew <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarFileNew"
|
|
||||||
menubarEditCut <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarEditCut"
|
|
||||||
menubarEditCopy <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarEditCopy"
|
|
||||||
menubarEditRename <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarEditRename"
|
|
||||||
menubarEditPaste <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarEditPaste"
|
|
||||||
menubarEditDelete <- builderGetObject builder castToImageMenuItem
|
|
||||||
"menubarEditDelete"
|
|
||||||
menubarHelpAbout <- builderGetObject builder castToImageMenuItem
|
menubarHelpAbout <- builderGetObject builder castToImageMenuItem
|
||||||
"menubarHelpAbout"
|
"menubarHelpAbout"
|
||||||
urlBar <- builderGetObject builder castToEntry
|
|
||||||
"urlBar"
|
|
||||||
statusBar <- builderGetObject builder castToStatusbar
|
statusBar <- builderGetObject builder castToStatusbar
|
||||||
"statusBar"
|
"statusBar"
|
||||||
clearStatusBar <- builderGetObject builder castToButton
|
clearStatusBar <- builderGetObject builder castToButton
|
||||||
"clearStatusBar"
|
"clearStatusBar"
|
||||||
rcMenu <- builderGetObject builder castToMenu
|
fpropGrid <- builderGetObject builder castToGrid
|
||||||
"rcMenu"
|
"fpropGrid"
|
||||||
rcFileOpen <- builderGetObject builder castToImageMenuItem
|
fpropFnEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileOpen"
|
"fpropFnEntry"
|
||||||
rcFileExecute <- builderGetObject builder castToImageMenuItem
|
fpropLocEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileExecute"
|
"fpropLocEntry"
|
||||||
rcFileNew <- builderGetObject builder castToImageMenuItem
|
fpropTsEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileNew"
|
"fpropTsEntry"
|
||||||
rcFileCut <- builderGetObject builder castToImageMenuItem
|
fpropModEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileCut"
|
"fpropModEntry"
|
||||||
rcFileCopy <- builderGetObject builder castToImageMenuItem
|
fpropAcEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileCopy"
|
"fpropAcEntry"
|
||||||
rcFileRename <- builderGetObject builder castToImageMenuItem
|
fpropFTEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileRename"
|
"fpropFTEntry"
|
||||||
rcFilePaste <- builderGetObject builder castToImageMenuItem
|
fpropPermEntry <- builderGetObject builder castToEntry
|
||||||
"rcFilePaste"
|
"fpropPermEntry"
|
||||||
rcFileDelete <- builderGetObject builder castToImageMenuItem
|
fpropLDEntry <- builderGetObject builder castToEntry
|
||||||
"rcFileDelete"
|
"fpropLDEntry"
|
||||||
upViewB <- builderGetObject builder castToButton
|
notebook1 <- builderGetObject builder castToNotebook
|
||||||
"upViewB"
|
"notebook1"
|
||||||
homeViewB <- builderGetObject builder castToButton
|
notebook2 <- builderGetObject builder castToNotebook
|
||||||
"homeViewB"
|
"notebook2"
|
||||||
refreshViewB <- builderGetObject builder castToButton
|
leftNbIcon <- builderGetObject builder castToImage
|
||||||
"refreshViewB"
|
"leftNbIcon"
|
||||||
menubarViewTree <- builderGetObject builder castToImageMenuItem
|
rightNbIcon <- builderGetObject builder castToImage
|
||||||
"menubarViewTree"
|
"rightNbIcon"
|
||||||
menubarViewIcon <- builderGetObject builder castToImageMenuItem
|
leftNbBtn <- builderGetObject builder castToToggleButton
|
||||||
"menubarViewIcon"
|
"leftNbBtn"
|
||||||
|
rightNbBtn <- builderGetObject builder castToToggleButton
|
||||||
|
"rightNbBtn"
|
||||||
|
|
||||||
|
|
||||||
|
-- this is required so that hotkeys work as expected, because
|
||||||
|
-- we then can connect to signals from `viewBox` more reliably
|
||||||
|
widgetSetCanFocus notebook1 False
|
||||||
|
widgetSetCanFocus notebook2 False
|
||||||
|
|
||||||
|
-- notebook toggle buttons
|
||||||
|
buttonSetImage leftNbBtn leftNbIcon
|
||||||
|
buttonSetImage rightNbBtn rightNbIcon
|
||||||
|
widgetSetSensitive leftNbIcon False
|
||||||
|
widgetSetSensitive rightNbIcon False
|
||||||
|
toggleButtonSetActive leftNbBtn True
|
||||||
|
toggleButtonSetActive rightNbBtn True
|
||||||
|
|
||||||
-- construct the gui object
|
-- construct the gui object
|
||||||
|
let menubar = MkMenuBar {..}
|
||||||
|
let fprop = MkFilePropertyGrid {..}
|
||||||
let mygui = MkMyGUI {..}
|
let mygui = MkMyGUI {..}
|
||||||
|
|
||||||
-- sets the default icon
|
-- sets the default icon
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ along with this program; if not, write to the Free Software
|
|||||||
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
--}
|
--}
|
||||||
|
|
||||||
{-# LANGUAGE OverloadedStrings #-}
|
{-# LANGUAGE RecordWildCards #-}
|
||||||
{-# OPTIONS_HADDOCK ignore-exports #-}
|
|
||||||
|
|
||||||
|
|
||||||
module HSFM.GUI.Gtk.MyView where
|
module HSFM.GUI.Gtk.MyView where
|
||||||
@@ -34,10 +33,15 @@ import Control.Concurrent.STM
|
|||||||
newTVarIO
|
newTVarIO
|
||||||
, readTVarIO
|
, readTVarIO
|
||||||
)
|
)
|
||||||
import Control.Exception
|
import Control.Monad
|
||||||
(
|
(
|
||||||
try
|
unless
|
||||||
, SomeException
|
, void
|
||||||
|
, when
|
||||||
|
)
|
||||||
|
import Control.Monad.IO.Class
|
||||||
|
(
|
||||||
|
liftIO
|
||||||
)
|
)
|
||||||
import Data.Foldable
|
import Data.Foldable
|
||||||
(
|
(
|
||||||
@@ -48,23 +52,26 @@ import Data.Maybe
|
|||||||
catMaybes
|
catMaybes
|
||||||
, fromJust
|
, fromJust
|
||||||
)
|
)
|
||||||
import Graphics.UI.Gtk
|
import Data.String
|
||||||
import {-# SOURCE #-} HSFM.GUI.Gtk.Callbacks (setCallbacks)
|
|
||||||
import HPath
|
|
||||||
(
|
(
|
||||||
Path
|
fromString
|
||||||
, Abs
|
|
||||||
)
|
)
|
||||||
|
import Graphics.UI.Gtk
|
||||||
|
import {-# SOURCE #-} HSFM.GUI.Gtk.Callbacks (setViewCallbacks)
|
||||||
import qualified HPath as P
|
import qualified HPath as P
|
||||||
import HSFM.FileSystem.FileOperations
|
|
||||||
import HSFM.FileSystem.FileType
|
import HSFM.FileSystem.FileType
|
||||||
import HSFM.GUI.Glib.GlibString()
|
import HSFM.GUI.Glib.GlibString()
|
||||||
import HSFM.GUI.Gtk.Data
|
import HSFM.GUI.Gtk.Data
|
||||||
import HSFM.GUI.Gtk.Icons
|
import HSFM.GUI.Gtk.Icons
|
||||||
import HSFM.GUI.Gtk.Utils
|
import HSFM.GUI.Gtk.Utils
|
||||||
|
import HSFM.History
|
||||||
import HSFM.Utils.IO
|
import HSFM.Utils.IO
|
||||||
|
import Paths_hsfm
|
||||||
|
(
|
||||||
|
getDataFileName
|
||||||
|
)
|
||||||
import Prelude hiding(readFile)
|
import Prelude hiding(readFile)
|
||||||
import System.INotify.ByteString
|
import System.INotify
|
||||||
(
|
(
|
||||||
addWatch
|
addWatch
|
||||||
, initINotify
|
, initINotify
|
||||||
@@ -73,21 +80,73 @@ import System.INotify.ByteString
|
|||||||
)
|
)
|
||||||
import System.IO.Error
|
import System.IO.Error
|
||||||
(
|
(
|
||||||
tryIOError
|
catchIOError
|
||||||
|
, ioError
|
||||||
|
, isUserError
|
||||||
|
)
|
||||||
|
import System.Posix.FilePath
|
||||||
|
(
|
||||||
|
hiddenFile
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- |Creates a new tab with its own view and refreshes the view.
|
||||||
|
newTab :: MyGUI -> Notebook -> IO FMView -> Item -> Int -> IO MyView
|
||||||
|
newTab mygui nb iofmv item pos = do
|
||||||
|
|
||||||
|
|
||||||
|
-- create eventbox with label
|
||||||
|
label <- labelNewWithMnemonic
|
||||||
|
(maybe (P.fromAbs $ path item) P.fromRel $ P.basename $ path item)
|
||||||
|
ebox <- eventBoxNew
|
||||||
|
eventBoxSetVisibleWindow ebox False
|
||||||
|
containerAdd ebox label
|
||||||
|
widgetShowAll label
|
||||||
|
|
||||||
|
myview <- createMyView mygui nb iofmv
|
||||||
|
_ <- notebookInsertPageMenu (notebook myview) (viewBox myview)
|
||||||
|
ebox ebox pos
|
||||||
|
|
||||||
|
-- set initial history
|
||||||
|
let historySize = 5
|
||||||
|
putMVar (history myview)
|
||||||
|
(BrowsingHistory [] (path item) [] historySize)
|
||||||
|
|
||||||
|
notebookSetTabReorderable (notebook myview) (viewBox myview) True
|
||||||
|
|
||||||
|
catchIOError (refreshView mygui myview item) $ \e -> do
|
||||||
|
file <- pathToFile getFileInfo . fromJust . P.parseAbs . fromString
|
||||||
|
$ "/"
|
||||||
|
refreshView mygui myview file
|
||||||
|
labelSetText label (fromString "/" :: String)
|
||||||
|
unless (isUserError e) (ioError e)
|
||||||
|
|
||||||
|
-- close callback
|
||||||
|
_ <- ebox `on` buttonPressEvent $ do
|
||||||
|
eb <- eventButton
|
||||||
|
case eb of
|
||||||
|
MiddleButton -> liftIO $ do
|
||||||
|
n <- notebookGetNPages (notebook myview)
|
||||||
|
when (n > 1) $ void $ destroyView myview
|
||||||
|
return True
|
||||||
|
_ -> return False
|
||||||
|
|
||||||
|
return myview
|
||||||
|
|
||||||
|
|
||||||
-- |Constructs the initial MyView object with a few dummy models.
|
-- |Constructs the initial MyView object with a few dummy models.
|
||||||
-- It also initializes the callbacks.
|
-- It also initializes the callbacks.
|
||||||
createMyView :: MyGUI
|
createMyView :: MyGUI
|
||||||
|
-> Notebook
|
||||||
-> IO FMView
|
-> IO FMView
|
||||||
-> IO MyView
|
-> IO MyView
|
||||||
createMyView mygui iofmv = do
|
createMyView mygui nb iofmv = do
|
||||||
operationBuffer <- newTVarIO None
|
|
||||||
|
|
||||||
inotify <- newEmptyMVar
|
inotify <- newEmptyMVar
|
||||||
|
history <- newEmptyMVar
|
||||||
|
|
||||||
|
builder <- builderNew
|
||||||
|
builderAddFromFile builder =<< getDataFileName "data/Gtk/builder.xml"
|
||||||
|
|
||||||
-- create dummy models, so we don't have to use MVar
|
-- create dummy models, so we don't have to use MVar
|
||||||
rawModel <- newTVarIO =<< listStoreNew []
|
rawModel <- newTVarIO =<< listStoreNew []
|
||||||
@@ -99,14 +158,35 @@ createMyView mygui iofmv = do
|
|||||||
view' <- iofmv
|
view' <- iofmv
|
||||||
view <- newTVarIO view'
|
view <- newTVarIO view'
|
||||||
|
|
||||||
|
urlBar <- builderGetObject builder castToEntry
|
||||||
|
"urlBar"
|
||||||
|
|
||||||
|
backViewB <- builderGetObject builder castToButton
|
||||||
|
"backViewB"
|
||||||
|
upViewB <- builderGetObject builder castToButton
|
||||||
|
"upViewB"
|
||||||
|
forwardViewB <- builderGetObject builder castToButton
|
||||||
|
"forwardViewB"
|
||||||
|
homeViewB <- builderGetObject builder castToButton
|
||||||
|
"homeViewB"
|
||||||
|
refreshViewB <- builderGetObject builder castToButton
|
||||||
|
"refreshViewB"
|
||||||
|
scroll <- builderGetObject builder castToScrolledWindow
|
||||||
|
"mainScroll"
|
||||||
|
viewBox <- builderGetObject builder castToBox
|
||||||
|
"viewBox"
|
||||||
|
|
||||||
|
let notebook = nb
|
||||||
let myview = MkMyView {..}
|
let myview = MkMyView {..}
|
||||||
|
|
||||||
-- set the bindings
|
-- set the bindings
|
||||||
setCallbacks mygui myview
|
setViewCallbacks mygui myview
|
||||||
|
|
||||||
-- add the treeview to the scroll container
|
-- add the treeview to the scroll container
|
||||||
let oview = fmViewToContainer view'
|
let oview = fmViewToContainer view'
|
||||||
containerAdd (scroll mygui) oview
|
containerAdd scroll oview
|
||||||
|
|
||||||
|
widgetShowAll viewBox
|
||||||
|
|
||||||
return myview
|
return myview
|
||||||
|
|
||||||
@@ -115,22 +195,42 @@ createMyView mygui iofmv = do
|
|||||||
-- io action returns.
|
-- io action returns.
|
||||||
switchView :: MyGUI -> MyView -> IO FMView -> IO ()
|
switchView :: MyGUI -> MyView -> IO FMView -> IO ()
|
||||||
switchView mygui myview iofmv = do
|
switchView mygui myview iofmv = do
|
||||||
|
cwd <- getCurrentDir myview
|
||||||
|
|
||||||
|
let nb = notebook myview
|
||||||
|
|
||||||
|
oldpage <- destroyView myview
|
||||||
|
|
||||||
|
-- create new view and tab page where the previous one was
|
||||||
|
nview <- newTab mygui nb iofmv cwd oldpage
|
||||||
|
|
||||||
|
page <- fromJust <$> notebookPageNum nb (viewBox nview)
|
||||||
|
notebookSetCurrentPage nb page
|
||||||
|
|
||||||
|
refreshView mygui nview cwd
|
||||||
|
|
||||||
|
|
||||||
|
-- |Destroys the given view by disconnecting the watcher
|
||||||
|
-- and destroying the active FMView container.
|
||||||
|
--
|
||||||
|
-- Everything that needs to be done in order to forget about a
|
||||||
|
-- view needs to be done here.
|
||||||
|
--
|
||||||
|
-- Returns the page in the tab list this view corresponds to.
|
||||||
|
destroyView :: MyView -> IO Int
|
||||||
|
destroyView myview = do
|
||||||
|
-- disconnect watcher
|
||||||
|
mi <- tryTakeMVar (inotify myview)
|
||||||
|
for_ mi $ \i -> killINotify i
|
||||||
|
|
||||||
|
page <- fromJust <$> notebookPageNum (notebook myview) (viewBox myview)
|
||||||
|
|
||||||
|
-- destroy old view and tab page
|
||||||
view' <- readTVarIO $ view myview
|
view' <- readTVarIO $ view myview
|
||||||
let oview = fmViewToContainer view'
|
widgetDestroy (fmViewToContainer view')
|
||||||
|
notebookRemovePage (notebook myview) page
|
||||||
|
|
||||||
widgetDestroy oview
|
return page
|
||||||
|
|
||||||
nview' <- iofmv
|
|
||||||
let nview = fmViewToContainer nview'
|
|
||||||
|
|
||||||
writeTVarIO (view myview) nview'
|
|
||||||
|
|
||||||
setCallbacks mygui myview
|
|
||||||
|
|
||||||
containerAdd (scroll mygui) nview
|
|
||||||
widgetShow nview
|
|
||||||
|
|
||||||
refreshView mygui myview Nothing
|
|
||||||
|
|
||||||
|
|
||||||
-- |Createss an IconView.
|
-- |Createss an IconView.
|
||||||
@@ -156,6 +256,13 @@ createTreeView = do
|
|||||||
tvs <- treeViewGetSelection treeView
|
tvs <- treeViewGetSelection treeView
|
||||||
treeSelectionSetMode tvs SelectionMultiple
|
treeSelectionSetMode tvs SelectionMultiple
|
||||||
|
|
||||||
|
-- set drag and drop
|
||||||
|
tl <- targetListNew
|
||||||
|
atom <- atomNew ("HSFM" :: String)
|
||||||
|
targetListAdd tl atom [TargetSameApp] 0
|
||||||
|
treeViewEnableModelDragDest treeView tl [ActionCopy]
|
||||||
|
treeViewEnableModelDragSource treeView [Button1] tl [ActionCopy]
|
||||||
|
|
||||||
-- create final tree model columns
|
-- create final tree model columns
|
||||||
renderTxt <- cellRendererTextNew
|
renderTxt <- cellRendererTextNew
|
||||||
renderPix <- cellRendererPixbufNew
|
renderPix <- cellRendererPixbufNew
|
||||||
@@ -197,57 +304,25 @@ createTreeView = do
|
|||||||
return $ FMTreeView treeView
|
return $ FMTreeView treeView
|
||||||
|
|
||||||
|
|
||||||
-- |Re-reads the current directory or the given one and updates the View.
|
|
||||||
-- This is more or less a wrapper around `refreshView'`
|
|
||||||
--
|
|
||||||
-- If the third argument is Nothing, it tries to re-read the current directory.
|
|
||||||
-- If that fails, it reads "/" instead.
|
|
||||||
--
|
|
||||||
-- If the third argument is (Just path) it tries to read "path". If that
|
|
||||||
-- fails, it reads "/" instead.
|
|
||||||
refreshView :: MyGUI
|
|
||||||
-> MyView
|
|
||||||
-> Maybe (Path Abs)
|
|
||||||
-> IO ()
|
|
||||||
refreshView mygui myview mfp =
|
|
||||||
case mfp of
|
|
||||||
Just fp -> do
|
|
||||||
-- readFileWithFileInfo can just outright fail...
|
|
||||||
ecdir <- tryIOError (readFile getFileInfo fp)
|
|
||||||
case ecdir of
|
|
||||||
Right cdir ->
|
|
||||||
-- ...or return an `AnchordFile` with a Failed constructor,
|
|
||||||
-- both of which need to be handled here
|
|
||||||
if (failed cdir)
|
|
||||||
then refreshView mygui myview =<< getAlternativeDir
|
|
||||||
else refreshView' mygui myview cdir
|
|
||||||
Left _ -> refreshView mygui myview =<< getAlternativeDir
|
|
||||||
Nothing -> refreshView mygui myview =<< getAlternativeDir
|
|
||||||
where
|
|
||||||
getAlternativeDir = do
|
|
||||||
ecd <- try (getCurrentDir myview) :: IO (Either SomeException
|
|
||||||
Item)
|
|
||||||
case ecd of
|
|
||||||
Right dir -> return (Just $ path dir)
|
|
||||||
Left _ -> return (P.parseAbs "/")
|
|
||||||
|
|
||||||
|
|
||||||
-- |Refreshes the View based on the given directory.
|
-- |Refreshes the View based on the given directory.
|
||||||
--
|
--
|
||||||
-- If the directory is not a Dir or a Symlink pointing to a Dir, then
|
-- Throws:
|
||||||
-- calls `refreshView` with the 3rd argument being Nothing.
|
--
|
||||||
refreshView' :: MyGUI
|
-- - `userError` on inappropriate type
|
||||||
|
refreshView :: MyGUI
|
||||||
-> MyView
|
-> MyView
|
||||||
-> Item
|
-> Item
|
||||||
-> IO ()
|
-> IO ()
|
||||||
refreshView' mygui myview dt@(DirOrSym _) = do
|
refreshView mygui myview SymLink { sdest = Just d@Dir{} } =
|
||||||
newRawModel <- fileListStore dt myview
|
refreshView mygui myview d
|
||||||
|
refreshView mygui myview item@Dir{} = do
|
||||||
|
newRawModel <- fileListStore item myview
|
||||||
writeTVarIO (rawModel myview) newRawModel
|
writeTVarIO (rawModel myview) newRawModel
|
||||||
|
|
||||||
view' <- readTVarIO $ view myview
|
view' <- readTVarIO $ view myview
|
||||||
|
|
||||||
_ <- tryTakeMVar (cwd myview)
|
_ <- tryTakeMVar (cwd myview)
|
||||||
putMVar (cwd myview) dt
|
putMVar (cwd myview) item
|
||||||
|
|
||||||
-- get selected items
|
-- get selected items
|
||||||
tps <- getSelectedTreePaths mygui myview
|
tps <- getSelectedTreePaths mygui myview
|
||||||
@@ -263,8 +338,7 @@ refreshView' mygui myview dt@(DirOrSym _) = do
|
|||||||
ntps <- mapM treeRowReferenceGetPath trs
|
ntps <- mapM treeRowReferenceGetPath trs
|
||||||
mapM_ (treeSelectionSelectPath tvs) ntps
|
mapM_ (treeSelectionSelectPath tvs) ntps
|
||||||
_ -> return ()
|
_ -> return ()
|
||||||
refreshView' mygui myview Failed{} = refreshView mygui myview Nothing
|
refreshView _ _ _ = ioError $ userError "Inappropriate type!"
|
||||||
refreshView' _ _ _ = return ()
|
|
||||||
|
|
||||||
|
|
||||||
-- |Constructs the visible View with the current underlying mutable models,
|
-- |Constructs the visible View with the current underlying mutable models,
|
||||||
@@ -289,17 +363,17 @@ constructView mygui myview = do
|
|||||||
dirtreePix FileLike{} = filePix
|
dirtreePix FileLike{} = filePix
|
||||||
dirtreePix DirSym{} = folderSymPix
|
dirtreePix DirSym{} = folderSymPix
|
||||||
dirtreePix FileLikeSym{} = fileSymPix
|
dirtreePix FileLikeSym{} = fileSymPix
|
||||||
dirtreePix Failed{} = errorPix
|
|
||||||
dirtreePix BrokenSymlink{} = errorPix
|
dirtreePix BrokenSymlink{} = errorPix
|
||||||
dirtreePix _ = errorPix
|
dirtreePix _ = errorPix
|
||||||
|
|
||||||
|
|
||||||
view' <- readTVarIO $ view myview
|
view' <- readTVarIO $ view myview
|
||||||
|
|
||||||
cdirp <- path <$> getCurrentDir myview
|
cdir <- getCurrentDir myview
|
||||||
|
let cdirp = path cdir
|
||||||
|
|
||||||
-- update urlBar
|
-- update urlBar
|
||||||
entrySetText (urlBar mygui) (P.fromAbs cdirp)
|
entrySetText (urlBar myview) (P.fromAbs cdirp)
|
||||||
|
|
||||||
rawModel' <- readTVarIO $ rawModel myview
|
rawModel' <- readTVarIO $ rawModel myview
|
||||||
|
|
||||||
@@ -311,7 +385,7 @@ constructView mygui myview = do
|
|||||||
item <- treeModelGetRow rawModel' iter >>= (P.basename . path)
|
item <- treeModelGetRow rawModel' iter >>= (P.basename . path)
|
||||||
if hidden
|
if hidden
|
||||||
then return True
|
then return True
|
||||||
else return $ not . P.hiddenFile $ item
|
else return . not . hiddenFile . P.fromRel $ item
|
||||||
|
|
||||||
-- sorting
|
-- sorting
|
||||||
sortedModel' <- treeModelSortNewWithModel filteredModel'
|
sortedModel' <- treeModelSortNewWithModel filteredModel'
|
||||||
@@ -337,7 +411,7 @@ constructView mygui myview = do
|
|||||||
-- update model of view
|
-- update model of view
|
||||||
case view' of
|
case view' of
|
||||||
FMTreeView treeView -> do
|
FMTreeView treeView -> do
|
||||||
treeViewSetModel treeView sortedModel'
|
treeViewSetModel treeView (Just sortedModel')
|
||||||
treeViewSetRubberBanding treeView True
|
treeViewSetRubberBanding treeView True
|
||||||
FMIconView iconView -> do
|
FMIconView iconView -> do
|
||||||
iconViewSetModel iconView (Just sortedModel')
|
iconViewSetModel iconView (Just sortedModel')
|
||||||
@@ -354,7 +428,7 @@ constructView mygui myview = do
|
|||||||
newi
|
newi
|
||||||
[Move, MoveIn, MoveOut, MoveSelf, Create, Delete, DeleteSelf]
|
[Move, MoveIn, MoveOut, MoveSelf, Create, Delete, DeleteSelf]
|
||||||
(P.fromAbs cdirp)
|
(P.fromAbs cdirp)
|
||||||
(\_ -> postGUIAsync $ refreshView mygui myview (Just $ cdirp))
|
(\_ -> postGUIAsync $ refreshView mygui myview cdir)
|
||||||
putMVar (inotify myview) newi
|
putMVar (inotify myview) newi
|
||||||
|
|
||||||
return ()
|
return ()
|
||||||
|
|||||||
112
src/HSFM/GUI/Gtk/Plugins.hs
Normal file
112
src/HSFM/GUI/Gtk/Plugins.hs
Normal file
@@ -0,0 +1,112 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
|
||||||
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
{-# OPTIONS_GHC -Wno-unused-imports #-}
|
||||||
|
|
||||||
|
|
||||||
|
module HSFM.GUI.Gtk.Plugins where
|
||||||
|
|
||||||
|
|
||||||
|
import Graphics.UI.Gtk
|
||||||
|
import HPath
|
||||||
|
import HSFM.FileSystem.FileType
|
||||||
|
import HSFM.GUI.Gtk.Data
|
||||||
|
import HSFM.GUI.Gtk.Settings
|
||||||
|
import HSFM.GUI.Gtk.Utils
|
||||||
|
import HSFM.Settings
|
||||||
|
import Control.Monad
|
||||||
|
(
|
||||||
|
forM
|
||||||
|
, forM_
|
||||||
|
, void
|
||||||
|
)
|
||||||
|
import System.Posix.Process.ByteString
|
||||||
|
(
|
||||||
|
executeFile
|
||||||
|
, forkProcess
|
||||||
|
)
|
||||||
|
import Data.ByteString.UTF8
|
||||||
|
(
|
||||||
|
fromString
|
||||||
|
)
|
||||||
|
import qualified Data.ByteString as BS
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---------------
|
||||||
|
--[ Plugins ]--
|
||||||
|
---------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- Global settings ----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- |Where to start inserting plugins.
|
||||||
|
insertPos :: Int
|
||||||
|
insertPos = 4
|
||||||
|
|
||||||
|
|
||||||
|
-- |A list of plugins to add to the right-click menu at position
|
||||||
|
-- `insertPos`.
|
||||||
|
--
|
||||||
|
-- The left part of the triple is a function that returns the menuitem.
|
||||||
|
-- The middle part of the triple is a filter function that
|
||||||
|
-- decides whether the item is shown.
|
||||||
|
-- The right part of the triple is the callback, which is invoked
|
||||||
|
-- when the menu item is clicked.
|
||||||
|
--
|
||||||
|
-- Plugins are added in order of this list.
|
||||||
|
myplugins :: [(IO MenuItem
|
||||||
|
,[Item] -> MyGUI -> MyView -> IO Bool
|
||||||
|
,[Item] -> MyGUI -> MyView -> IO ())
|
||||||
|
]
|
||||||
|
myplugins = [(diffItem, diffFilter, diffCallback)
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- The plugins ----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
diffItem :: IO MenuItem
|
||||||
|
diffItem = menuItemNewWithLabel "diff"
|
||||||
|
|
||||||
|
diffFilter :: [Item] -> MyGUI -> MyView -> IO Bool
|
||||||
|
diffFilter items _ _
|
||||||
|
| length items > 1 = return $ and $ fmap isFileC items
|
||||||
|
| otherwise = return False
|
||||||
|
|
||||||
|
diffCallback :: [Item] -> MyGUI -> MyView -> IO ()
|
||||||
|
diffCallback items _ _ = void $
|
||||||
|
forkProcess $
|
||||||
|
executeFile
|
||||||
|
(fromString "meld")
|
||||||
|
True
|
||||||
|
([fromString "--diff"] ++ fmap (fromAbs . path) items)
|
||||||
|
Nothing
|
||||||
|
|
||||||
128
src/HSFM/GUI/Gtk/Settings.hs
Normal file
128
src/HSFM/GUI/Gtk/Settings.hs
Normal file
@@ -0,0 +1,128 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-# LANGUAGE PatternSynonyms #-}
|
||||||
|
|
||||||
|
|
||||||
|
module HSFM.GUI.Gtk.Settings where
|
||||||
|
|
||||||
|
|
||||||
|
import Graphics.UI.Gtk
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
--------------------
|
||||||
|
--[ GUI Settings ]--
|
||||||
|
--------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- Hotkey settings ----
|
||||||
|
|
||||||
|
|
||||||
|
pattern QuitModifier :: [Modifier]
|
||||||
|
pattern QuitModifier <- [Control]
|
||||||
|
|
||||||
|
pattern QuitKey :: String
|
||||||
|
pattern QuitKey <- "q"
|
||||||
|
|
||||||
|
|
||||||
|
pattern ShowHiddenModifier :: [Modifier]
|
||||||
|
pattern ShowHiddenModifier <- [Control]
|
||||||
|
|
||||||
|
pattern ShowHiddenKey :: String
|
||||||
|
pattern ShowHiddenKey <- "h"
|
||||||
|
|
||||||
|
|
||||||
|
pattern UpDirModifier :: [Modifier]
|
||||||
|
pattern UpDirModifier <- [Alt]
|
||||||
|
|
||||||
|
pattern UpDirKey :: String
|
||||||
|
pattern UpDirKey <- "Up"
|
||||||
|
|
||||||
|
|
||||||
|
pattern HistoryBackModifier :: [Modifier]
|
||||||
|
pattern HistoryBackModifier <- [Alt]
|
||||||
|
|
||||||
|
pattern HistoryBackKey :: String
|
||||||
|
pattern HistoryBackKey <- "Left"
|
||||||
|
|
||||||
|
|
||||||
|
pattern HistoryForwardModifier :: [Modifier]
|
||||||
|
pattern HistoryForwardModifier <- [Alt]
|
||||||
|
|
||||||
|
pattern HistoryForwardKey :: String
|
||||||
|
pattern HistoryForwardKey <- "Right"
|
||||||
|
|
||||||
|
|
||||||
|
pattern DeleteModifier :: [Modifier]
|
||||||
|
pattern DeleteModifier <- []
|
||||||
|
|
||||||
|
pattern DeleteKey :: String
|
||||||
|
pattern DeleteKey <- "Delete"
|
||||||
|
|
||||||
|
|
||||||
|
pattern OpenModifier :: [Modifier]
|
||||||
|
pattern OpenModifier <- []
|
||||||
|
|
||||||
|
pattern OpenKey :: String
|
||||||
|
pattern OpenKey <- "Return"
|
||||||
|
|
||||||
|
|
||||||
|
pattern CopyModifier :: [Modifier]
|
||||||
|
pattern CopyModifier <- [Control]
|
||||||
|
|
||||||
|
pattern CopyKey :: String
|
||||||
|
pattern CopyKey <- "c"
|
||||||
|
|
||||||
|
|
||||||
|
pattern MoveModifier :: [Modifier]
|
||||||
|
pattern MoveModifier <- [Control]
|
||||||
|
|
||||||
|
pattern MoveKey :: String
|
||||||
|
pattern MoveKey <- "x"
|
||||||
|
|
||||||
|
|
||||||
|
pattern PasteModifier :: [Modifier]
|
||||||
|
pattern PasteModifier <- [Control]
|
||||||
|
|
||||||
|
pattern PasteKey :: String
|
||||||
|
pattern PasteKey <- "v"
|
||||||
|
|
||||||
|
|
||||||
|
pattern NewTabModifier :: [Modifier]
|
||||||
|
pattern NewTabModifier <- [Control]
|
||||||
|
|
||||||
|
pattern NewTabKey :: String
|
||||||
|
pattern NewTabKey <- "t"
|
||||||
|
|
||||||
|
|
||||||
|
pattern CloseTabModifier :: [Modifier]
|
||||||
|
pattern CloseTabModifier <- [Control]
|
||||||
|
|
||||||
|
pattern CloseTabKey :: String
|
||||||
|
pattern CloseTabKey <- "w"
|
||||||
|
|
||||||
|
|
||||||
|
pattern OpenTerminalModifier :: [Modifier]
|
||||||
|
pattern OpenTerminalModifier <- []
|
||||||
|
|
||||||
|
pattern OpenTerminalKey :: String
|
||||||
|
pattern OpenTerminalKey <- "F4"
|
||||||
|
|
||||||
@@ -67,24 +67,7 @@ getSelectedItems :: MyGUI
|
|||||||
-> IO [Item]
|
-> IO [Item]
|
||||||
getSelectedItems mygui myview = do
|
getSelectedItems mygui myview = do
|
||||||
tps <- getSelectedTreePaths mygui myview
|
tps <- getSelectedTreePaths mygui myview
|
||||||
getSelectedItems' mygui myview tps
|
catMaybes <$> mapM (rawPathToItem myview) tps
|
||||||
|
|
||||||
|
|
||||||
getSelectedItems' :: MyGUI
|
|
||||||
-> MyView
|
|
||||||
-> [TreePath]
|
|
||||||
-> IO [Item]
|
|
||||||
getSelectedItems' _ myview tps = do
|
|
||||||
rawModel' <- readTVarIO $ rawModel myview
|
|
||||||
sortedModel' <- readTVarIO $ sortedModel myview
|
|
||||||
filteredModel' <- readTVarIO $ filteredModel myview
|
|
||||||
iters <- catMaybes <$> mapM (treeModelGetIter sortedModel') tps
|
|
||||||
forM iters $ \iter -> do
|
|
||||||
cIter' <- treeModelSortConvertIterToChildIter sortedModel' iter
|
|
||||||
cIter <- treeModelFilterConvertIterToChildIter filteredModel' cIter'
|
|
||||||
treeModelGetRow rawModel' cIter
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- |Carry out an action on the currently selected item.
|
-- |Carry out an action on the currently selected item.
|
||||||
@@ -95,8 +78,8 @@ withItems :: MyGUI
|
|||||||
-> ( [Item]
|
-> ( [Item]
|
||||||
-> MyGUI
|
-> MyGUI
|
||||||
-> MyView
|
-> MyView
|
||||||
-> IO ()) -- ^ action to carry out
|
-> IO a) -- ^ action to carry out
|
||||||
-> IO ()
|
-> IO a
|
||||||
withItems mygui myview io = do
|
withItems mygui myview io = do
|
||||||
items <- getSelectedItems mygui myview
|
items <- getSelectedItems mygui myview
|
||||||
io items mygui myview
|
io items mygui myview
|
||||||
@@ -124,13 +107,14 @@ getFirstItem myview = do
|
|||||||
|
|
||||||
|
|
||||||
-- |Reads the current directory from MyView.
|
-- |Reads the current directory from MyView.
|
||||||
|
--
|
||||||
|
-- This reads the MVar and may block the main thread if it's
|
||||||
|
-- empty.
|
||||||
getCurrentDir :: MyView
|
getCurrentDir :: MyView
|
||||||
-> IO Item
|
-> IO Item
|
||||||
getCurrentDir myview = readMVar (cwd myview)
|
getCurrentDir myview = readMVar (cwd myview)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- |Push a message to the status bar.
|
-- |Push a message to the status bar.
|
||||||
pushStatusBar :: MyGUI -> String -> IO (ContextId, MessageId)
|
pushStatusBar :: MyGUI -> String -> IO (ContextId, MessageId)
|
||||||
pushStatusBar mygui str = do
|
pushStatusBar mygui str = do
|
||||||
@@ -146,3 +130,25 @@ popStatusbar mygui = do
|
|||||||
let sb = statusBar mygui
|
let sb = statusBar mygui
|
||||||
cid <- statusbarGetContextId sb "FM Status"
|
cid <- statusbarGetContextId sb "FM Status"
|
||||||
statusbarPop sb cid
|
statusbarPop sb cid
|
||||||
|
|
||||||
|
|
||||||
|
-- |Turn a path on the rawModel into a path that we can
|
||||||
|
-- use at the outermost model layer.
|
||||||
|
rawPathToIter :: MyView -> TreePath -> IO (Maybe TreeIter)
|
||||||
|
rawPathToIter myview tp = do
|
||||||
|
fmodel <- readTVarIO (filteredModel myview)
|
||||||
|
smodel <- readTVarIO (sortedModel myview)
|
||||||
|
msiter <- treeModelGetIter smodel tp
|
||||||
|
forM msiter $ \siter -> do
|
||||||
|
cIter <- treeModelSortConvertIterToChildIter smodel siter
|
||||||
|
treeModelFilterConvertIterToChildIter fmodel cIter
|
||||||
|
|
||||||
|
|
||||||
|
-- |Turn a path on the rawModel into the corresponding item
|
||||||
|
-- that we can use at the outermost model layer.
|
||||||
|
rawPathToItem :: MyView -> TreePath -> IO (Maybe Item)
|
||||||
|
rawPathToItem myview tp = do
|
||||||
|
rawModel' <- readTVarIO $ rawModel myview
|
||||||
|
miter <- rawPathToIter myview tp
|
||||||
|
forM miter $ \iter -> treeModelGetRow rawModel' iter
|
||||||
|
|
||||||
|
|||||||
61
src/HSFM/History.hs
Normal file
61
src/HSFM/History.hs
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
|
module HSFM.History where
|
||||||
|
|
||||||
|
|
||||||
|
import HPath
|
||||||
|
(
|
||||||
|
Abs
|
||||||
|
, Path
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- |Browsing history. For `forwardHistory` and `backwardsHistory`
|
||||||
|
-- the first item is the most recent one.
|
||||||
|
data BrowsingHistory = BrowsingHistory {
|
||||||
|
backwardsHistory :: [Path Abs]
|
||||||
|
, currentDir :: Path Abs
|
||||||
|
, forwardHistory :: [Path Abs]
|
||||||
|
, maxSize :: Int
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
-- |This is meant to be called after e.g. a new path is entered
|
||||||
|
-- (not navigated to via the history) and the history needs updating.
|
||||||
|
historyNewPath :: Path Abs -> BrowsingHistory -> BrowsingHistory
|
||||||
|
historyNewPath p (BrowsingHistory b cd _ s) =
|
||||||
|
BrowsingHistory (take s $ cd:b) p [] s
|
||||||
|
|
||||||
|
|
||||||
|
-- |Go back one step in the history.
|
||||||
|
historyBack :: BrowsingHistory -> BrowsingHistory
|
||||||
|
historyBack bh@(BrowsingHistory [] _ _ _) = bh
|
||||||
|
historyBack (BrowsingHistory (b:bs) cd fs s) =
|
||||||
|
BrowsingHistory bs b (take s $ cd:fs) s
|
||||||
|
|
||||||
|
|
||||||
|
-- |Go forward one step in the history.
|
||||||
|
historyForward :: BrowsingHistory -> BrowsingHistory
|
||||||
|
historyForward bh@(BrowsingHistory _ _ [] _) = bh
|
||||||
|
historyForward (BrowsingHistory bs cd (f:fs) s) =
|
||||||
|
BrowsingHistory (take s $ cd:bs) f fs s
|
||||||
|
|
||||||
67
src/HSFM/Settings.hs
Normal file
67
src/HSFM/Settings.hs
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
{--
|
||||||
|
HSFM, a filemanager written in Haskell.
|
||||||
|
Copyright (C) 2016 Julian Ospald
|
||||||
|
|
||||||
|
This program is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU General Public License
|
||||||
|
version 2 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 General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with this program; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||||
|
--}
|
||||||
|
|
||||||
|
{-# OPTIONS_HADDOCK ignore-exports #-}
|
||||||
|
|
||||||
|
|
||||||
|
module HSFM.Settings where
|
||||||
|
|
||||||
|
|
||||||
|
import Data.ByteString
|
||||||
|
(
|
||||||
|
ByteString
|
||||||
|
)
|
||||||
|
import qualified Data.ByteString.UTF8 as BU
|
||||||
|
import Data.Maybe
|
||||||
|
import System.Posix.Env.ByteString
|
||||||
|
import System.Posix.Process.ByteString
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-----------------------
|
||||||
|
--[ Common Settings ]--
|
||||||
|
-----------------------
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
---- Command settings ----
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
-- |The terminal command. This should call `executeFile` in the end
|
||||||
|
-- with the appropriate arguments.
|
||||||
|
terminalCommand :: ByteString -- ^ current directory of the FM
|
||||||
|
-> IO a
|
||||||
|
terminalCommand cwd =
|
||||||
|
executeFile -- executes the given command
|
||||||
|
(BU.fromString "sakura") -- the terminal command
|
||||||
|
True -- whether to search PATH
|
||||||
|
[BU.fromString "-d", cwd] -- arguments for the command
|
||||||
|
Nothing -- optional custom environment: `Just [(String, String)]`
|
||||||
|
|
||||||
|
|
||||||
|
-- |The home directory. If you want to set it explicitly, you might
|
||||||
|
-- want to do:
|
||||||
|
--
|
||||||
|
-- @
|
||||||
|
-- home = return "\/home\/wurst"
|
||||||
|
-- @
|
||||||
|
home :: IO ByteString
|
||||||
|
home = fromMaybe <$> return (BU.fromString "/") <*> getEnv (BU.fromString "HOME")
|
||||||
|
|
||||||
@@ -33,11 +33,6 @@ import Control.Concurrent.STM.TVar
|
|||||||
, modifyTVar
|
, modifyTVar
|
||||||
, TVar
|
, TVar
|
||||||
)
|
)
|
||||||
import Control.Monad
|
|
||||||
(
|
|
||||||
when
|
|
||||||
, unless
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
-- |Atomically write a TVar.
|
-- |Atomically write a TVar.
|
||||||
@@ -49,14 +44,3 @@ writeTVarIO tvar val = atomically $ writeTVar tvar val
|
|||||||
modifyTVarIO :: TVar a -> (a -> a) -> IO ()
|
modifyTVarIO :: TVar a -> (a -> a) -> IO ()
|
||||||
modifyTVarIO tvar f = atomically $ modifyTVar tvar f
|
modifyTVarIO tvar f = atomically $ modifyTVar tvar f
|
||||||
|
|
||||||
|
|
||||||
-- |If the value of the first argument is True, then execute the action
|
|
||||||
-- provided in the second argument, otherwise do nothing.
|
|
||||||
whenM :: Monad m => m Bool -> m () -> m ()
|
|
||||||
whenM mb a = mb >>= (`when` a)
|
|
||||||
|
|
||||||
|
|
||||||
-- |If the value of the first argument is False, then execute the action
|
|
||||||
-- provided in the second argument, otherwise do nothing.
|
|
||||||
unlessM :: Monad m => m Bool -> m () -> m ()
|
|
||||||
unlessM mb a = mb >>= (`unless` a)
|
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|||||||
module HSFM.Utils.MyPrelude where
|
module HSFM.Utils.MyPrelude where
|
||||||
|
|
||||||
|
|
||||||
import Data.Default
|
|
||||||
import Data.List
|
import Data.List
|
||||||
|
|
||||||
|
|
||||||
@@ -31,6 +30,3 @@ listIndices :: [a] -> [Int]
|
|||||||
listIndices = findIndices (const True)
|
listIndices = findIndices (const True)
|
||||||
|
|
||||||
|
|
||||||
-- |A `maybe` flavor using the `Default` class.
|
|
||||||
maybeD :: (Default b) => (a -> b) -> Maybe a -> b
|
|
||||||
maybeD = maybe def
|
|
||||||
|
|||||||
52
update-gh-pages.sh
Executable file
52
update-gh-pages.sh
Executable file
@@ -0,0 +1,52 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
SOURCE_BRANCH="master"
|
||||||
|
TARGET_BRANCH="gh-pages"
|
||||||
|
REPO="https://${GH_TOKEN}@github.com/hasufell/hsfm"
|
||||||
|
DOC_LOCATION="/dist/doc/html/hsfm/hsfm-gtk"
|
||||||
|
|
||||||
|
|
||||||
|
# Pull requests and commits to other branches shouldn't try to deploy,
|
||||||
|
# just build to verify
|
||||||
|
if [ "$TRAVIS_PULL_REQUEST" != "false" -o "$TRAVIS_BRANCH" != "$SOURCE_BRANCH" ]; then
|
||||||
|
echo "Skipping docs deploy."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
cd "$HOME"
|
||||||
|
git config --global user.email "travis@travis-ci.org"
|
||||||
|
git config --global user.name "travis-ci"
|
||||||
|
git clone --branch=${TARGET_BRANCH} ${REPO} ${TARGET_BRANCH} || exit 1
|
||||||
|
|
||||||
|
# docs
|
||||||
|
cd ${TARGET_BRANCH} || exit 1
|
||||||
|
echo "Removing old docs."
|
||||||
|
rm -rf *
|
||||||
|
echo "Adding new docs."
|
||||||
|
cp -rf "${TRAVIS_BUILD_DIR}${DOC_LOCATION}"/* . || exit 1
|
||||||
|
|
||||||
|
# If there are no changes to the compiled out (e.g. this is a README update)
|
||||||
|
# then just bail.
|
||||||
|
if [ -z "`git diff --exit-code`" ]; then
|
||||||
|
echo "No changes to the output on this push; exiting."
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
git add -- .
|
||||||
|
|
||||||
|
if [[ -e ./index.html ]] ; then
|
||||||
|
echo "Commiting docs."
|
||||||
|
git commit -m "Lastest docs updated
|
||||||
|
|
||||||
|
travis build: $TRAVIS_BUILD_NUMBER
|
||||||
|
commit: $TRAVIS_COMMIT
|
||||||
|
auto-pushed to gh-pages"
|
||||||
|
|
||||||
|
git push origin $TARGET_BRANCH
|
||||||
|
echo "Published docs to gh-pages."
|
||||||
|
else
|
||||||
|
echo "Error: docs are empty."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
66
update-index-state.sh
Executable file
66
update-index-state.sh
Executable file
@@ -0,0 +1,66 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
status_message() {
|
||||||
|
printf "\\033[0;32m%s\\033[0m\\n" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
error_message() {
|
||||||
|
printf "\\033[0;31m%s\\033[0m\\n" "$1"
|
||||||
|
}
|
||||||
|
|
||||||
|
SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
|
||||||
|
CACHE_LOCATION="${HOME}/.cabal/packages/hackage.haskell.org/01-index.cache"
|
||||||
|
|
||||||
|
if [ ! -f "${CACHE_LOCATION}" ] ; then
|
||||||
|
error_message "${CACHE_LOCATION} does not exist, did you run 'cabal update'?"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ ! -f "${SCRIPTPATH}/cabal.project" ] ; then
|
||||||
|
error_message "Could not find ${SCRIPTPATH}/cabal.project, skipping index state update."
|
||||||
|
exit 3
|
||||||
|
fi
|
||||||
|
|
||||||
|
cabal v2-update
|
||||||
|
|
||||||
|
arch=$(getconf LONG_BIT)
|
||||||
|
|
||||||
|
case "${arch}" in
|
||||||
|
32)
|
||||||
|
byte_size=4
|
||||||
|
magic_word="CABA1002"
|
||||||
|
;;
|
||||||
|
64)
|
||||||
|
byte_size=8
|
||||||
|
magic_word="00000000CABA1002"
|
||||||
|
;;
|
||||||
|
*)
|
||||||
|
error_message "Unknown architecture (long bit): ${arch}"
|
||||||
|
exit 2
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
# This is the logic to parse the binary format of 01-index.cache.
|
||||||
|
# The first word is a magic 'caba1002', the second one is the timestamp in unix epoch.
|
||||||
|
# Better than copying the cabal-install source code.
|
||||||
|
if [ "$(xxd -u -p -l${byte_size} -s 0 "${CACHE_LOCATION}")" != "${magic_word}" ] ; then
|
||||||
|
error_message "Magic word does not match!"
|
||||||
|
exit 4
|
||||||
|
fi
|
||||||
|
cache_timestamp=$(echo "ibase=16;obase=A;$(xxd -u -p -l${byte_size} -s ${byte_size} "${CACHE_LOCATION}")" | bc)
|
||||||
|
|
||||||
|
# If we got junk from the binary file, this should fail.
|
||||||
|
cache_date=$(date --utc --date "@${cache_timestamp}" "+%FT%TZ")
|
||||||
|
|
||||||
|
|
||||||
|
status_message "Updating index state in ${SCRIPTPATH}/cabal.project"
|
||||||
|
|
||||||
|
if grep -q "^index-state: .*" "${SCRIPTPATH}/cabal.project" ; then
|
||||||
|
awk '/index-state:/ {gsub(/.*/, "index-state: '${cache_date}'")}; { print }' "${SCRIPTPATH}/cabal.project" > "${SCRIPTPATH}/cabal.project.tmp"
|
||||||
|
mv "${SCRIPTPATH}/cabal.project.tmp" "${SCRIPTPATH}/cabal.project"
|
||||||
|
else
|
||||||
|
printf "index-state: %s\n" "${cache_date}" >> "${SCRIPTPATH}/cabal.project"
|
||||||
|
fi
|
||||||
|
|
||||||
Reference in New Issue
Block a user