soduto / Soduto

Soduto is a KDEConnect compatible client for macOS. It allows better integration between your phones, desktops and tablets.
https://www.soduto.com
GNU General Public License v3.0
319 stars 49 forks source link

Get the PNG icon from the payload #29

Closed sannidhyaroy closed 10 months ago

sannidhyaroy commented 1 year ago

KDE Connect sends the app icon in PNG format as a payload along with the notification data packet. How can I get the image in Soduto? I tried adding a getAppIcon() method inside the DataPacket extension in the NotificationsService.swift file:

func getAppIcon() throws -> Data? {
  try self.validateNotificationType()
  guard hasPayload() else { return nil }

  guard let payloadStream = payload else { return nil }

  payloadStream.open()
  defer {
      payloadStream.close()
  }

  let bufferSize = 1024
  let buffer = UnsafeMutablePointer<UInt8>.allocate(capacity: bufferSize)
  defer {
      buffer.deallocate()
  }

  var imageData = Data()

  while payloadStream.hasBytesAvailable {
      let bytesRead = payloadStream.read(buffer, maxLength: bufferSize)
      if bytesRead < 0 {
          // Stream error occured
          throw payloadStream.streamError!
      } else if bytesRead == 0 {
          // End of buffer was reached
          break
      }
      imageData.append(buffer, count: bytesRead)
  }

  guard imageData.count == payloadSize else { return nil }

  return imageData
}

However, when I access this method from the showNotification() function by defining let appIcon = try dataPacket.getAppIcon(), nothing happens. It returns nil for the line guard let payloadStream = payload else { return nil } in the getAppIcon() method.

@giedrius-stanevicius Where am I going wrong? I agree I'm not very good with Swift and I didn't work with InputStream or similar data types in any other language either. Obviously the function above isn't entirely written by me, thanks to StackOverflow. Could you guide me a bit? I've been trying to get this to work for a couple of months but can't figure it out.

giedrius-stanevicius commented 1 year ago

I don't remember the codebase, so I may be wrong, but it seems you should use dataPacket.downloadTask, not dataPacket.payload. You can check the ShareService for example how to use it.

sidevesh commented 11 months ago

@sannidhyaroy @giedrius-stanevicius https://github.com/sannidhyaroy/Soduto/pull/18 I have raised this PR with some logic added towards getting the notification icon from the app show up.

sidevesh commented 10 months ago

@sannidhyaroy I think you can close this since we have figured out how to get the PNG icon from the payload

sannidhyaroy commented 10 months ago

@sannidhyaroy I think you can close this since we have figured out how to get the PNG icon from the payload

Sure, thank you for the help.