Add local file provider.
This commit is contained in:
29
src/fileAccess.nim
Normal file
29
src/fileAccess.nim
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
import os, sets, random
|
||||||
|
type
|
||||||
|
FileProvider* = ref object
|
||||||
|
exts: HashSet[string]
|
||||||
|
path*: string
|
||||||
|
files*: seq[string]
|
||||||
|
|
||||||
|
proc load*(fp: FileProvider) =
|
||||||
|
## Reload the file list
|
||||||
|
if fp.path == "":
|
||||||
|
return
|
||||||
|
|
||||||
|
for file in walkDirRec(fp.path):
|
||||||
|
let split = splitFile(file)
|
||||||
|
if fp.exts.contains(split.ext):
|
||||||
|
fp.files.add(file)
|
||||||
|
|
||||||
|
randomize()
|
||||||
|
shuffle(fp.files)
|
||||||
|
|
||||||
|
proc next*(fp: FileProvider): string =
|
||||||
|
if fp.files.len < 1:
|
||||||
|
fp.load
|
||||||
|
result = fp.files[0]
|
||||||
|
fp.files.delete(0)
|
||||||
|
|
||||||
|
proc newFileProvider*(path: string): FileProvider =
|
||||||
|
result = FileProvider(path: path, exts: [".png", ".jpg", ".jpeg"].toHashSet)
|
||||||
|
result.load
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
import httpClient, json, os, options, strformat
|
import httpClient, json, os, options, strformat
|
||||||
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
|
import gintro/[gtk, glib, gobject, gio, gdkpixbuf]
|
||||||
import argparse except run
|
import argparse except run
|
||||||
|
import fileAccess
|
||||||
|
|
||||||
const
|
const
|
||||||
version = "0.1"
|
version = "0.1"
|
||||||
@@ -10,6 +11,7 @@ type
|
|||||||
Mode {.pure.} = enum
|
Mode {.pure.} = enum
|
||||||
Foxes = "foxes" ## Some nice foxes
|
Foxes = "foxes" ## Some nice foxes
|
||||||
Inspiro = "inspiro" ## Inspiring nonsense
|
Inspiro = "inspiro" ## Inspiring nonsense
|
||||||
|
File = "file" ## Images from a local path
|
||||||
|
|
||||||
var
|
var
|
||||||
client = newHttpClient()
|
client = newHttpClient()
|
||||||
@@ -17,9 +19,11 @@ var
|
|||||||
imageWidget: Image
|
imageWidget: Image
|
||||||
fullscreen = true
|
fullscreen = true
|
||||||
mode: Option[Mode]
|
mode: Option[Mode]
|
||||||
|
fileProvider: FileProvider
|
||||||
argParser = newParser("randopix"):
|
argParser = newParser("randopix"):
|
||||||
help(fmt"Version {version} - Display random images from different sources")
|
help(fmt"Version {version} - Display random images from different sources")
|
||||||
option("-m", "--mode", help="foxes, inspiro, inspiro-xmas")
|
option("-m", "--mode", help="foxes, file, inspiro, inspiro-xmas")
|
||||||
|
option("-p", "--path", help="('file' mode only) Path to a directory with images")
|
||||||
flag("-w", "--windowed", help="Do not start in fullscreen mode")
|
flag("-w", "--windowed", help="Do not start in fullscreen mode")
|
||||||
|
|
||||||
proc downloadFox(): Pixbuf =
|
proc downloadFox(): Pixbuf =
|
||||||
@@ -30,6 +34,11 @@ proc downloadFox(): Pixbuf =
|
|||||||
discard loader.write(imageData)
|
discard loader.write(imageData)
|
||||||
loader.getPixbuf()
|
loader.getPixbuf()
|
||||||
|
|
||||||
|
|
||||||
|
proc getLocalImage(): Pixbuf =
|
||||||
|
## let the file provider serve another image
|
||||||
|
fileProvider.next.newPixbufFromFile
|
||||||
|
|
||||||
proc resizeImage(pixbuf: Pixbuf): Pixbuf =
|
proc resizeImage(pixbuf: Pixbuf): Pixbuf =
|
||||||
var wWidth, wHeight, width, height: int
|
var wWidth, wHeight, width, height: int
|
||||||
window.getSize(wWidth, wHeight)
|
window.getSize(wWidth, wHeight)
|
||||||
@@ -50,6 +59,8 @@ proc getImage(): Option[Pixbuf] =
|
|||||||
result = some(downloadFox())
|
result = some(downloadFox())
|
||||||
of Mode.Inspiro:
|
of Mode.Inspiro:
|
||||||
echo "Not Implemented"
|
echo "Not Implemented"
|
||||||
|
of Mode.File:
|
||||||
|
result = some(getLocalImage())
|
||||||
|
|
||||||
proc updateImage(action: SimpleAction; parameter: Variant;) =
|
proc updateImage(action: SimpleAction; parameter: Variant;) =
|
||||||
let data = getImage();
|
let data = getImage();
|
||||||
@@ -101,6 +112,9 @@ proc parseArgs(): void =
|
|||||||
except ValueError:
|
except ValueError:
|
||||||
echo "Invaild image source: ", opts.mode
|
echo "Invaild image source: ", opts.mode
|
||||||
|
|
||||||
|
if (mode.isSome and mode.get == Mode.File):
|
||||||
|
fileProvider = newFileProvider(opts.path)
|
||||||
|
|
||||||
proc appActivate(app: Application) =
|
proc appActivate(app: Application) =
|
||||||
parseArgs()
|
parseArgs()
|
||||||
# No mode was given, exit and display the help text
|
# No mode was given, exit and display the help text
|
||||||
@@ -109,7 +123,7 @@ proc appActivate(app: Application) =
|
|||||||
return
|
return
|
||||||
|
|
||||||
window = newApplicationWindow(app)
|
window = newApplicationWindow(app)
|
||||||
window.title = "Randopics"
|
window.title = "randopix"
|
||||||
window.setKeepAbove(true)
|
window.setKeepAbove(true)
|
||||||
window.setDefaultSize(600, 600)
|
window.setDefaultSize(600, 600)
|
||||||
|
|
||||||
@@ -126,6 +140,6 @@ proc appActivate(app: Application) =
|
|||||||
window.showAll
|
window.showAll
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
let app = newApplication("org.gtk.example")
|
let app = newApplication("org.luxick.randopix")
|
||||||
connect(app, "activate", appActivate)
|
connect(app, "activate", appActivate)
|
||||||
discard run(app)
|
discard run(app)
|
||||||
Reference in New Issue
Block a user